
use "created_dta/${analysissamp}", clear

forv i = 1/36 {
	gen amtssissdi_in_`i' = amtdisability_in_`i' - amtvadc_in_`i'
}	

*-------------------------*
*-------------------------*
*Appendix Tables
*------------------------*
*-------------------------*

*************
* Table A1   
*************

eststo clear

forvalues x = 2005/2015 {
	qui egen BCT`x' =  group(dmsl_tc_first) if year_arrival == `x'
	qui summ BCT`x'
	estadd scalar mBCT`x' = r(max)
	qui egen POST`x' =  group(post_orgraa) if year_arrival == `x'
	qui summ POST`x'
	estadd scalar mPOST`x' = r(max)	
}													

	esttab using "${outputfolder}\TableA1.tex", ///
		 replace noobs nomtitle nodepvar nonumber plain fragment nolabel collabels("Num of BCTs(Posts)") ///
		varlabels(, none) ///
		mlabel(, none) ///
		stats(mBCT2005 mPOST2005 mBCT2006 mPOST2006 mBCT2007 mPOST2007 mBCT2008 mPOST2008 mBCT2009 mPOST2009 mBCT2010 mPOST2010 mBCT2011 mPOST2011 mBCT2012 mPOST2012 mBCT2013 mPOST2013 mBCT2014 mPOST2014 mBCT2015 mPOST2015,layout(`""@ (@)""' `""@ (@)""' `""@ (@)""' `""@ (@)""' `""@ (@)""' `""@ (@)""' `""@ (@)""' `""@ (@)""' `""@ (@)""' `""@ (@)""' `""@ (@)""') labels("2005" "2006" "2007" "2008" "2009" "2010" "2011" "2012" "2013" "2014" "2015"))


*************
* Table A2 
*************
	
	global runlist combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_ noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ bar_or_misconduct_by_ inc_combined_by_  vantage_v4_score731 nsc_enr_pa_by_2020 nsc_asscplus_pa_by_2020
	
qui {
	local loop = 0
	eststo clear
	foreach fe in FE1 {
	foreach dv in $runlist {
		local loop = `loop' + 1 
	foreach x in 8 16 24 32 {
	if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
		eststo `dv'_`x' : reghdfe `dv'`x' ${NEWINST} if ${rest} >= `x' , absorb(${`fe'}) vce(robust)
	}
	if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
		eststo `dv'_`x' : reghdfe `dv' ${NEWINST} if ${rest} >= `x' , absorb(${`fe'}) vce(robust)
	}
	local `dv'`x'_b = _b[${NEWINST}] * 10
	local `dv'`x'_se = _se[${NEWINST}] * 10
	local `dv'`x'_N = e(N)

	if `x' == 32 { 
		if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
			reg `dv'`x' if e(sample)
		}
		if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
			reg `dv' if e(sample)
		}
		local `dv'_m = _b[_cons]
	}
	
	local dec 2
	
	local `dv'`x'_se: display %5.`dec'f ``dv'`x'_se'
	if ``dv'`x'_se' < 10 & `dec' == 2 local `dv'`x'_se = substr("``dv'`x'_se'",-4,.)
	local `dv'`x'_p = abs(``dv'`x'_b'/``dv'`x'_se')
	if ``dv'`x'_p' < 1.645 local `dv'`x'_star = ""
	if ``dv'`x'_p'>= 1.645 local `dv'`x'_star = "*"
	if ``dv'`x'_p'>= 1.96 local `dv'`x'_star = "**"
	if ``dv'`x'_p'>= 2.576 local `dv'`x'_star = "***"
	
	}

if"`dv'" =="combat_death_by_" global axislab"Combat Death"
if"`dv'" =="wia_by_" global axislab"Ever Combat Injury"
if"`dv'" =="any_sigprofile_by_" global axislab"Significant Army Profile"
if"`dv'" =="amtvadc_in_" global axislab"Annual Amt VADC"
if"`dv'" =="anyvadc_in_" global axislab"Any VADC Receipt"
if"`dv'" =="noncom_death_by_" global axislab"Noncombat Death"
if"`dv'" =="ndi_despair2_by_" global axislab"Death of Despair"
if"`dv'" =="ndi_suicide_plus2_by_" global axislab"Suicide"
if"`dv'" =="ndi_drug2_by_" global axislab"Drug- or Alcohol-Rel. Death"
if"`dv'" =="ndi_mv_acc2_by_" global axislab"Motor Vehicle Death"
if"`dv'" =="bar_or_misconduct_by_" global axislab"Separated for Misconduct/Barred"
if"`dv'" =="inc_combined_by_" global axislab"Ever Incarcerated"
if"`dv'" =="vantage_v4_score731" global axislab"Credit Score in 2020 (Vantage)"
if"`dv'" =="nsc_enr_pa_by_2020" global axislab"Enrolled by 2020 (Post-Arrival)"
if"`dv'" =="nsc_asscplus_pa_by_2020" global axislab"Assc Deg+ by 2020 (Post-Arrival)"
	file open b using "${outputfolder}/TableA2_row`loop'.tex", write replace
		file write b "${axislab}" _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(38) %5.`dec'f (``dv'24_b') ("``dv'24_star'") _char(38) %5.`dec'f (``dv'32_b') ("``dv'32_star'") _char(38) %5.`dec'f (``dv'_m') _char(92) _char(92) _n
file write b "" _char(38) _char(40) "``dv'8_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(38) _char(40) "``dv'24_se'" _char(41) _char(38) _char(40) "``dv'32_se'" _char(41) _char(38) _char(92) _char(92) _n
		file write b "Observations" _char(38) "``dv'8_N'" _char(38) "``dv'16_N'" _char(38) "``dv'24_N'" _char(38) "``dv'32_N'" _char(38) "``dv'32_N'" _n
		file close b
}		
}
}



*************
* Table A3  
*************

*code below generates excel files, these are then cleaned up in python using prep_vcv.py which produces ests_formatlab.csv and finally this is inputted into matlab using est_frandsen.m and testjfe.m 

*commenting out as this is slow: 
/*
use "created_dta/${analysissamp}", clear
keep if ${rest}>= 32 
egen tempbctxQFE = group(dmsl_tc_first qtr)
levelsof tempbctxQFE, local(BCTxQTRlist)
foreach x in `BCTxQTRlist' {
	g bctXQFE`x' = tempbctxQFE == `x'
}
g samp1 = 1 
tempfile a 
save `a', replace

use "created_dta/${analysissamp}", clear
keep if ${rest}>= 32 
egen tempbctxQFE = group(dmsl_tc_first qtr)
levelsof tempbctxQFE, local(BCTxQTRlist)
foreach x in `BCTxQTRlist' {
	g bctXQFE`x' = tempbctxQFE == `x'
}
g samp2 = 1 
append using `a'

replace samp1 = 0 if samp1 == . 
replace samp2 = 0 if samp2 == .


foreach x in `BCTxQTRlist' {
	g samp2bctXQFE`x' = bctXQFE`x' * (samp2)
	g samp1bctXQFE`x' = bctXQFE`x' * (1-samp2)

}

egen sampFE = group($FE1 samp2)

	*reference group 
	drop bctXQFE615 
	drop samp2bctXQFE615 
	drop samp1bctXQFE615 

	reghdfe $NEWENDOG bctXQFE* if samp1, absorb($FE1) vce(robust) noconstant 

local list2 "615"
local list3: list BCTxQTRlist - list2 
	foreach k of local list3 { 
		if _b[bctXQFE`k'] == 0 drop bctXQFE`k' 
		if _b[bctXQFE`k'] == 0 drop samp2bctXQFE`k'
		if _b[bctXQFE`k'] == 0 drop samp1bctXQFE`k' 
	}
	

foreach outcome in combat_death_by_32 noncom_death_by_32 anyvadc_in_32 {  // 

cap drop mega_outcome 
g mega_outcome = $NEWENDOG * (1-samp2) + (samp2)*`outcome'  

*then run mega spec (this is slow)
reghdfe mega_outcome samp2bctXQFE* samp1bctXQFE*, absorb(sampFE samp2) cluster(pid) noconstant 
putexcel set "created_dta/excel_beta_cov_`outcome'", replace sheet(Sheet1)
putexcel A1=matrix(e(b)), names 
putexcel set "created_dta/excel_varcov_cov_`outcome'", replace sheet(Sheet1)
putexcel A1=matrix(e(V)), names 
} 
*/


*************
* Table A4 
*************
use "created_dta/${analysissamp}", clear

forv i = 1/36 {
	gen amtssissdi_in_`i' = amtdisability_in_`i' - amtvadc_in_`i'
}	

	gen sep_in_qtr=1 if num_qtr_allowed>=32 & sep_by_1==100
	forvalues k=2(1)32{
		local j=`k'-1
		replace sep_in_qtr=`k' if sep_by_`k'==100 & sep_by_`j'==0 & num_qtr_allowed>=32 
	}
	
	
global RUNLIST anyvadc_in_ amtvadc_in_ 

eststo clear
qui {
foreach fe in FE1 { // 
foreach dv in $RUNLIST {

foreach x in 32 {

	foreach n in wia_by any_sigprofile_by {
		replace `n'_12 = `n'_12/100
	}


// Estimate effect of an additional month deployed, conditional on any deployment
eststo `dv'`x'_conpos : reghdfe `dv'`x' ${NEWENDOG}  if ${rest} >= `x' & ${NEWENDOG}>0, absorb($FE1) vce(robust)
	local obs1 = e(N)
// Estimate effect of WIA (within 3 years) conditional on deploying (controlling for months deployed)
eststo `dv'`x'_wiadep : reghdfe `dv'`x' wia_by_12 ${NEWENDOG} if ${rest} >= `x' & ${NEWENDOG}>0, absorb($FE1) vce(robust)
	local obs2 = e(N)
// Estimate effect of Significant Medical Profile (within 3 years) conditional on deploying (controlling for months deployed)
eststo `dv'`x'_profdep : reghdfe `dv'`x' any_sigprofile_by_12 ${NEWENDOG} if ${rest} >= `x' & ${NEWENDOG}>0, absorb($FE1) vce(robust)
	local obs3 = e(N)
// Estimate same regressions as above, but with WIA, Sig Profile, and an interaction
gen wia_sigprofile_by_12=wia_by_12*any_sigprofile_by_12
eststo `dv'`x'_wiaprof : reghdfe `dv'`x' wia_by_12 any_sigprofile_by_12 wia_sigprofile_by_12 ${NEWENDOG} if ${rest} >= `x' & ${NEWENDOG}>0, absorb($FE1) vce(robust)
	local obs4 = e(N)
// Estimate same regressions as above, but with WIA, Sig Profile, and an interaction (conditional on separation)
eststo `dv'`x'_wiaprofs1 : reghdfe `dv'`x' wia_by_12 any_sigprofile_by_12 wia_sigprofile_by_12 ${NEWENDOG} if ${rest} >= `x' & ${NEWENDOG}>0 & sep_by_`x'==100, absorb($FE1) vce(robust)
	local obs5 = e(N)
	
	foreach n in wia_by any_sigprofile_by {
		replace `n'_12 = `n'_12*100
	}

drop wia_sigprofile_by_12

if "`dv'" == "amtvadc_in_" {
	local dec 2
	noi esttab  `dv'*  using "${outputfolder}\TableA4_panelb.tex"  ///
	, ///
	cells(b(fmt(`dec') star) se(par fmt(`dec')))  ///
	transform(${NEWENDOG} @*${SCALE} ${SCALE}) ///
	replace noobs nomtitle nodepvar nonumber plain fragment label ///
	substitute(_ \_) style(tex) ///
	mlabels(, none) collabels(, none) ///
	keep(${NEWENDOG} wia_by_12 any_sigprofile_by_12 wia_sigprofile_by_12, relax) ///
	varlabels(${NEWENDOG} "${NEWENDOGLAB}" wia_by_12 "Combat Injury by 3 Years" any_sigprofile_by_12 "Significant Army Profile by 3 Years" wia_sigprofile_by_12 "Combat Injury by 3 Years X Significant Army Profile by 3 Years") wrap ///
	starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30) ///
	postfoot("\hline" "Conditional on Deployment & X & X & X & X & X\\" "Conditional on Separation &&&& X\\" "Observations & `obs1' &  `obs2' &  `obs3' &  `obs4' &  `obs5'")
}
else {
	local dec 2
	noi esttab  `dv'*  using "${outputfolder}\TableA4_panela.tex"  ///
	, ///
	cells(b(fmt(`dec') star) se(par fmt(`dec')))  ///
	transform(${NEWENDOG} @*${SCALE} ${SCALE}) ///
	replace noobs nomtitle nodepvar nonumber plain fragment label ///
	substitute(_ \_) style(tex) ///
	mlabels(, none) collabels(, none) ///
	keep(${NEWENDOG} wia_by_12 any_sigprofile_by_12 wia_sigprofile_by_12, relax) ///
	varlabels(${NEWENDOG} "${NEWENDOGLAB}" wia_by_12 "Combat Injury by 3 Years" any_sigprofile_by_12 "Significant Army Profile by 3 Years" wia_sigprofile_by_12 "Combat Injury by 3 Years X Significant Army Profile by 3 Years") wrap ///
	starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30)
}


}
	
}
}
}



*************
* Table A5   
*************

merge 1:1 pid using raw_dta/vadc_diag_analysis_samp_ajh 
drop _merge 

foreach outcomes in any_tinitus_in any_knee_in any_ptsd_in any_back_in any_ankle_in  any_amputation_in {
	foreach x in 8 16 24 32 { 
	replace `outcomes'_`x' = 0 if `outcomes'_`x' == . & ${rest} >= `x'
	replace `outcomes'_`x' = `outcomes'_`x' * 100
	}
}

global RUNLIST any_tinitus_in_ any_knee_in_ any_ptsd_in_ any_back_in_ any_ankle_in_ wia_serplus_by_  wia_vser_by_ any_amputation_in_ anywl_in_ anyssdissi_in_ amtssissdi_in_

	foreach x in 8 16 24 32 { 
	summ wia_vser_by_`x'
	if r(max) != 100 {
	replace wia_vser_by_`x' = wia_vser_by_`x'*100
	}
	summ anywl_in_`x'
	if r(max) != 100 {
	replace anywl_in_`x' = anywl_in_`x'*100
	}
	}
	
local xlist 8 16 24 32
local xlist2 24 32 
eststo clear
local loop = 0
qui {
foreach dv in $RUNLIST {
	local loop = `loop' + 1 

	if "`dv'" == "any_tinitus_in_" local displab "Tinnitus"
	if "`dv'" == "any_knee_in_" local displab "Limitation of Flexion, Knee"
	if "`dv'" == "any_ptsd_in_" local displab "PTSD"
	if "`dv'" == "any_back_in_" local displab "Lumbosacral or Cervical Strain"
	if "`dv'" == "any_ankle_in_" local displab "Limitation of Motion of the Ankle"
	if "`dv'" == "wia_serplus_by_" local displab "Serious or Very Serious Combat Injury"
	if "`dv'" == "wia_vser_by_" local displab "Very Serious Combat Injury"
	if "`dv'" == "any_amputation_in_" local displab "Any VADC w/ Amputation"
	if "`dv'" == "anywl_in_" local displab "Any VADC IU or CDR 100"
	if "`dv'" == "anyssdissi_in_" local displab "Any SSI or SSDI"
	if "`dv'" == "amtssissdi_in_" local displab "Amount SSI or SSDI"

	
local whichx `xlist'
if "`dv'" == "any_amputation_in_" local whichx `xlist2'

foreach x in `whichx' {
*Replicate Existing Estimate With Single Instrument
eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
qui sum `dv'`x' if e(sample)
estadd scalar depmean = r(mean)
	if `x' == 32 { 
		cap drop t 
		cap drop o
		g t = ${NEWENDOG}
		replace ${NEWENDOG} = 1
		g o = `dv'`x'/${SCALE}
		eststo `dv'_99 :reg  o ${NEWENDOG} if e(sample), nocons
		replace ${NEWENDOG} = t
		drop t 
		drop o
	}
}
	
local dec 2
noi esttab  `dv'_* using "${outputfolder}\TableA5_row`loop'.tex"  ///
, ///
cells(b(fmt(`dec') star) se(par fmt(`dec')))  ///
transform( @*${SCALE} ${SCALE}) ///
replace noobs nomtitle nodepvar nonumber plain fragment label ///
substitute(_ \_) style(tex) ///
mlabels(, none) collabels(, none) ///
keep(${NEWENDOG}, relax) ///
varlabels(${NEWENDOG} "`displab'") ///
starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30) ///
stats(N, labels("\hline \\ Observations") fmt(%16.0gc))

}
}


*************
* Table A6   
*************
	
	global runlist sep_by_ volsep_by_ bar_or_misconduct_by_ disabsep_by_
		
	qui { 
	eststo clear
	local loop = 0 
	foreach fe in FE1 {
	foreach dv in $runlist {
		local loop = `loop' + 1 
	foreach x in 8 16 24 32 {
	eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb(${`fe'}) robust $ivregsettings
	
	local dec 2
	local `dv'`x'_b = _b[${NEWENDOG}] * 10
	local `dv'`x'_se = _se[${NEWENDOG}] * 10
	local `dv'`x'_se: display %5.`dec'f ``dv'`x'_se'
	if ``dv'`x'_se' < 10 & `dec' == 2 local `dv'`x'_se = substr("``dv'`x'_se'",-4,.)
	local `dv'`x'_N = e(N)

	if `x' == 32 { 
		reg `dv'`x' if e(sample)
		local `dv'_m = _b[_cons]
	}
	local `dv'`x'_p = abs(``dv'`x'_b'/``dv'`x'_se')
	if ``dv'`x'_p' < 1.645 local `dv'`x'_star = ""
	if ``dv'`x'_p'>= 1.645 local `dv'`x'_star = "*"
	if ``dv'`x'_p'>= 1.96 local `dv'`x'_star = "**"
	if ``dv'`x'_p'>= 2.576 local `dv'`x'_star = "***"
	
	}
qui {			
if "`dv'" == "sep_by_" global axislab "Ever Separated"
if "`dv'" == "volsep_by_" global axislab "Voluntarily Separated"
if "`dv'" == "bar_or_misconduct_by_" global axislab "Separated for Misconduct/Barred"
if "`dv'" == "disabsep_by_" global axislab "Separated for Disability"
}	

	file open b using "${outputfolder}/TableA6_row`loop'.tex", write replace
	file write b "${axislab}" _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(38) %5.`dec'f (``dv'24_b') ("``dv'24_star'") _char(38) %5.`dec'f (``dv'32_b') ("``dv'32_star'") _char(38) %5.`dec'f (``dv'_m') _char(92) _char(92) _n
file write b "" _char(38) _char(40) "``dv'8_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(38) _char(40) "``dv'24_se'" _char(41) _char(38) _char(40) "``dv'32_se'" _char(41) _char(38) _char(92) _char(92) "\hline" _n
file write b "Observations" _char(38) "``dv'8_N'" _char(38) "``dv'16_N'" _char(38) "``dv'24_N'" _char(38) "``dv'32_N'" _char(38) "``dv'32_N'" _n
	file close b
	
}
		
}
}



*************
* Table A7   
*************
	
	global runlist ndi_death_by_ combat_death_by_ noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ ndi_assault2_by_ ndi_other2_by_
	local loop = 0
	qui { 
	eststo clear
	foreach fe in FE1 {
	foreach dv in $runlist {
		local loop = `loop' + 1 
	foreach x in 8 16 24 32 {
	eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb(${`fe'}) robust $ivregsettings
	local `dv'`x'_N = e(N)
	local dec 2
	local `dv'`x'_b = _b[${NEWENDOG}] * 10
	local `dv'`x'_se = _se[${NEWENDOG}] * 10
	local `dv'`x'_se: display %5.`dec'f ``dv'`x'_se'
	if ``dv'`x'_se' < 10 & `dec' == 2 local `dv'`x'_se = substr("``dv'`x'_se'",-4,.)
	if `x' == 32 { 
		reg `dv'`x' if e(sample)
		local `dv'_m = _b[_cons]
	}
	local `dv'`x'_p = abs(``dv'`x'_b'/``dv'`x'_se')
	if ``dv'`x'_p' < 1.645 local `dv'`x'_star = ""
	if ``dv'`x'_p'>= 1.645 local `dv'`x'_star = "*"
	if ``dv'`x'_p'>= 1.96 local `dv'`x'_star = "**"
	if ``dv'`x'_p'>= 2.576 local `dv'`x'_star = "***"
	
	}

if "`dv'" == "ndi_death_by_" global axislab "Death"
if "`dv'" == "combat_death_by_" global axislab "Combat Death"
if "`dv'" == "noncom_death_by_" global axislab "Noncombat Death"
if "`dv'" == "ndi_despair2_by_" global axislab "Death of Despair"
if "`dv'" == "ndi_suicide_plus2_by_" global axislab "Suicide"
if "`dv'" == "ndi_drug2_by_" global axislab "Drug- or Alcohol-Rel. Death"
if "`dv'" == "ndi_mv_acc2_by_" global axislab "Motor Vehicle Death"
if "`dv'" == "ndi_assault2_by_" global axislab "Assault Death"
if "`dv'" == "ndi_other2_by_" global axislab "Other Noncombat Death"

	file open b using "${outputfolder}/TableA7_row`loop'.tex", write replace
	file write b "${axislab}" _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(38) %5.`dec'f (``dv'24_b') ("``dv'24_star'") _char(38) %5.`dec'f (``dv'32_b') ("``dv'32_star'") _char(38) %5.`dec'f (``dv'_m') _char(92) _char(92) _n
file write b "" _char(38) _char(40) "``dv'8_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(38) _char(40) "``dv'24_se'" _char(41) _char(38) _char(40) "``dv'32_se'" _char(41) _char(38) _char(92) _char(92) "\hline" _n
file write b "Observations" _char(38) "``dv'8_N'" _char(38) "``dv'16_N'" _char(38) "``dv'24_N'" _char(38) "``dv'32_N'" _char(38) "``dv'32_N'" _n
	file close b
	
}
		
}
}




*************
* Table A8 
*************

	global runlist demo_by_ bar_or_misconduct_by_ any_vf_by_ any_nvf_by_ any_mis_notraffic_by_ any_oth_by_ inc_combined_by_
	
	qui { 
	local loop = 0 
	eststo clear
	foreach fe in FE1 {
	foreach dv in $runlist {
		local loop = `loop' + 1 
	foreach x in 8 16 24 32 {
	eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb(${`fe'}) robust $ivregsettings
	local `dv'`x'_N = e(N)
	local `dv'`x'_b = _b[${NEWENDOG}] * 10
	local `dv'`x'_se = _se[${NEWENDOG}] * 10
	
	local dec 2
	if inlist("`dv'","ndi_assault2_by_") local dec 4
	if inlist("`dv'","ndi_despair2_by_") local dec 3
	if inlist("`dv'","amtdisability_in_","amtvadc_in_") local dec 2
	

	local `dv'`x'_se: display %5.`dec'f ``dv'`x'_se'
	if ``dv'`x'_se' < 10 & `dec' == 2 local `dv'`x'_se = substr("``dv'`x'_se'",-4,.)
	if `x' == 32 { 
		reg `dv'`x' if e(sample)
		local `dv'_m = _b[_cons]
	}
	local `dv'`x'_p = abs(``dv'`x'_b'/``dv'`x'_se')
	if ``dv'`x'_p' < 1.645 local `dv'`x'_star = ""
	if ``dv'`x'_p'>= 1.645 local `dv'`x'_star = "*"
	if ``dv'`x'_p'>= 1.96 local `dv'`x'_star = "**"
	if ``dv'`x'_p'>= 2.576 local `dv'`x'_star = "***"
	
	}

	
if "`dv'" == "demo_by_" global axislab "Ever Demoted"
if "`dv'" == "bar_or_misconduct_by_" global axislab "Separated for Misconduct/Barred"
if "`dv'" == "any_vf_by_" global axislab "Ever Violent Felony"
if "`dv'" == "any_nvf_by_" global axislab "Ever Non-Violent Felony"
if "`dv'" == "any_mis_notraffic_by_" global axislab "Ever Misdemeanor (Non-traffic)"
if "`dv'" == "any_oth_by_" global axislab "Ever Other Crime (Meta-category)"
if "`dv'" == "inc_combined_by_" global axislab "Ever Incarcerated"

	file open b using "${outputfolder}/TableA8_row`loop'.tex", write replace
	file write b "${axislab}" _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(38) %5.`dec'f (``dv'24_b') ("``dv'24_star'") _char(38) %5.`dec'f (``dv'32_b') ("``dv'32_star'") _char(38) %5.`dec'f (``dv'_m') _char(92) _char(92) _n
file write b "" _char(38) _char(40) %5.`dec'f "``dv'8_se'" _char(41) _char(38) _char(40) %5.`dec'f "``dv'16_se'" _char(41) _char(38) _char(40) %5.`dec'f "``dv'24_se'" _char(41) _char(38) _char(40) %5.`dec'f "``dv'32_se'" _char(41) _char(38) _char(92) _char(92) _n
file write b "Observations" _char(38) "``dv'8_N'" _char(38) "``dv'16_N'" _char(38) "``dv'24_N'" _char(38) "``dv'32_N'" _char(38) "``dv'32_N'" _n
	file close b

}
		
}
}



*************
* Table A9
*************
	merge 1:1 pid using "raw_dta\rec_any_forc_2019", nogen keepusing(rec_any_forc_by_2019)
	replace rec_any_forc_by_2019 = rec_any_forc_by_2019*100
	
	local runlist vantage_v3_score689 premier_v1_2_all5020689 premier_v1_2_mtf5020689 premier_v1_2_aua5020689 premier_v1_2_stu5020689 premier_v1_2_all5070689 premier_v1_2_col5060689 premier_v1_2_all2870689 vantage_v4_score731 fictbv10_score731 p13_all5020731 p13_mtf5020731 p13_aua5020731 p13_stu5020731 p13_all5070731 p13_col5060731 p13_all2870731 any_pgib_use any_pgib_use_by2017 any_pgib_use_by2020 rec_any_forc_by_2019
	
qui {
foreach dv in `runlist' {
	local prefix
	if inlist("`dv'","premier_v1_2_all2870689","p13_all2870731") local prefix = "any_"
	ivreghdfe `prefix'`dv' (${NEWENDOG} = ${NEWINST}) , absorb($FE1) robust $ivregsettings
	local `dv'_b = _b[${NEWENDOG}] * 10
	local `dv'_se = _se[${NEWENDOG}] * 10
	summ `prefix'`dv' if e(sample) ==1 
	local `dv'_m = r(mean)

	display "`dv'"
	
	local dec 2
	local `dv'_se: display %5.`dec'f ``dv'_se'
	if ``dv'_se' < 10 & `dec' == 2 local `dv'_se = substr("``dv'_se'",-4,.)
	
	local `dv'_p = abs(``dv'_b'/``dv'_se')
	if ``dv'_p' < 1.645 local `dv'_star = ""
	if ``dv'_p'>= 1.645 local `dv'_star = "*"
	if ``dv'_p'>= 1.96 local `dv'_star = "**"
	if ``dv'_p'>= 2.576 local `dv'_star = "***"
}
}

cap file close b

file open b using "${outputfolder}/TableA9_panela.tex", write replace
	file write b "Vantage Score" _char(38) %5.2f (`vantage_v3_score689_b') ("`vantage_v3_score689_star'")
	file write b _char(38) %5.1f (`vantage_v3_score689_m') _char(38) %5.2f (`vantage_v4_score731_b') ("`vantage_v3_score731_star'") _char(38) %5.1f (`vantage_v4_score731_m') _char(92) _char(92) _n
	file write b "" _char(38) _char(40) "`vantage_v3_score689_se'" _char(41) _char(38) _char(38) _char(40) "`vantage_v4_score731_se'" _char(41) _char(92) _char(92) _n

	file write b "FICO Score" _char(38) _char(38)  _char(38) %5.2f (`fictbv10_score731_b') ("`fictbv10_score731_star'") _char(38) %5.1f (`fictbv10_score731_m') _char(92) _char(92) _n
	file write b "" _char(38)  _char(38) _char(38) _char(40) "`fictbv10_score731_se'" _char(41) _char(92) _char(92) _n
file close b


file open b using "${outputfolder}/TableA9_panelb.tex", write replace
	local creditlist all5020 mtf5020 aua5020 stu5020  
	foreach dv in `creditlist' {
		if "`dv'" == "all5020" local dvlab "Total Debt"
		if "`dv'" == "mtf5020" local dvlab "Mortage Debt"
		if "`dv'" == "aua5020" local dvlab "Auto Debt"
		if "`dv'" == "stu5020" local dvlab "Student Debt"
		
		file write b "`dvlab'" _char(38) %9.0f (`premier_v1_2_`dv'689_b') ("`premier_v1_2_`dv'689_star'") _char(38) %9.0f (`premier_v1_2_`dv'689_m') _char(38) %9.0f (`p13_`dv'731_b') ("`p13_`dv'731_star'") _char(38) %9.0f (`p13_`dv'731_m') _char(92) _char(92) _n
	file write b "" _char(38) _char(40) "`premier_v1_2_`dv'689_se'" _char(41) _char(38) _char(38) _char(40) "`p13_`dv'731_se'" _char(41) _char(92) _char(92) _n
	
	}
file close b


file open b using "${outputfolder}/TableA9_panelc.tex", write replace
	local creditlist all5070 col5060 all2870 rec_any_forc_by_2019

	foreach dv in `creditlist' {
	local bdec 2
	local mdec 1
	
	display "`dv'"
	
	if "`dv'" == "all5070" local dvlab "Derogatory Debt"
	if "`dv'" == "col5060" local dvlab "Debt in Colleciton"
	if "`dv'" == "all2870" local dvlab "Any Bankruptcy"
	if "`dv'" == "rec_any_forc_by_2019" local dvlab "Any Foreclosure Action"

	if "`dv'" != "rec_any_forc_by_2019" {
		file write b "`dvlab'" _char(38) %5.`bdec'f (`premier_v1_2_`dv'689_b') ("`premier_v1_2_`dv'689_star'") _char(38) %5.`mdec'f (`premier_v1_2_`dv'689_m') _char(38) %5.`bdec'f (`p13_`dv'731_b') ("`p13_`dv'731_star'") _char(38) %5.`mdec'f (`p13_`dv'731_m') _char(92) _char(92) _n
		file write b "" _char(38) _char(40) "`premier_v1_2_`dv'689_se'" _char(41) _char(38) _char(38) _char(40) "`p13_`dv'731_se'" _char(41) _char(92) _char(92) _n
	}

	if "`dv'" == "rec_any_forc_by_2019" {
		display "hello"
		file write b "`dvlab'" _char(38) _char(38) _char(38) %5.`bdec'f (``dv'_b') ("``dv'_star'") _char(38) %5.`mdec'f (``dv'_m') _char(92) _char(92) _n
		file write b "" _char(38) _char(38) _char(38) _char(40) "``dv'_se'" _char(41) _char(92) _char(92) _n
	}
	
		
}

file close b
	


*************
* Table A10 
*************
	
	global runlist   nsc_enr_postarr_by_ nsc_asscplus_postarr_by_ nsc_bachplus_postarr_by_
	
	qui { 
	local loop = 0 
	eststo clear
	foreach fe in FE1{
	foreach dv in $runlist {
		local loop = `loop' + 1 
	foreach x in 8 16 24 32 {
	eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb(${`fe'}) robust $ivregsettings
	local `dv'`x'_b = _b[${NEWENDOG}] * 10
	local `dv'`x'_se = _se[${NEWENDOG}] * 10
	local `dv'`x'_N = e(N)

	local dec 2 
	if `x' == 32 { 
		reg `dv'`x' if e(sample)
		local `dv'_m = _b[_cons]
	}
	local `dv'`x'_se: display %5.`dec'f ``dv'`x'_se'
	if ``dv'`x'_se' < 10 & `dec' == 2 local `dv'`x'_se = substr("``dv'`x'_se'",-4,.)
	
	local `dv'`x'_p = abs(``dv'`x'_b'/``dv'`x'_se')
	if ``dv'`x'_p' < 1.645 local `dv'`x'_star = ""
	if ``dv'`x'_p'>= 1.645 local `dv'`x'_star = "*"
	if ``dv'`x'_p'>= 1.96 local `dv'`x'_star = "**"
	if ``dv'`x'_p'>= 2.576 local `dv'`x'_star = "***"
	
	}
	
if "`dv'" == "nsc_enr_postarr_by_" global axislab "Enroll Post-arrival"
if "`dv'" == "nsc_asscplus_postarr_by_" global axislab "Assc Deg+ Post-arrival"
if "`dv'" == "nsc_bachplus_postarr_by_" global axislab "Bach Deg+ Post-arrival"

	file open b using "${outputfolder}/TableA10_row`loop'.tex", write replace
		file write b "${axislab}" _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(38) %5.`dec'f (``dv'24_b') ("``dv'24_star'") _char(38) %5.`dec'f (``dv'32_b') ("``dv'32_star'") _char(38) %5.`dec'f (``dv'_m') _char(92) _char(92) _n
file write b "" _char(38) _char(40) "``dv'8_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(38) _char(40) "``dv'24_se'" _char(41) _char(38) _char(40) "``dv'32_se'" _char(41) _char(38) _char(92) _char(92) "\hline" _n
file write b "Observations" _char(38) "``dv'8_N'" _char(38) "``dv'16_N'" _char(38) "``dv'24_N'" _char(38) "``dv'32_N'" _char(38) "``dv'32_N'" _n
	file close b
	
}
}
}

	
	
local runlist nsc_enr_pa_by_2017 nsc_enr_pa_by_2020 nsc_asscplus_pa_by_2017 nsc_asscplus_pa_by_2020 nsc_bachplus_pa_by_2017 nsc_bachplus_pa_by_2020 any_pgib_use_by2017 any_pgib_use_by2020
qui {
foreach dv in `runlist' {
	ivreghdfe `dv' (${NEWENDOG} = ${NEWINST}) , absorb($FE1) robust $ivregsettings
	local `dv'_b = _b[${NEWENDOG}] * 10
	local `dv'_se = _se[${NEWENDOG}] * 10
	summ `dv' if e(sample) ==1 
	local `dv'_m = r(mean)
	local `dv'_N = e(N)
	
	local dec 2
	local `dv'_se: display %5.`dec'f ``dv'_se'
	if ``dv'_se' < 10 & `dec' == 2 local `dv'_se = substr("``dv'_se'",-4,.)
	
	local `dv'_p = abs(``dv'_b'/``dv'_se')
	if ``dv'_p' < 1.645 local `dv'_star = ""
	if ``dv'_p'>= 1.645 local `dv'_star = "*"
	if ``dv'_p'>= 1.96 local `dv'_star = "**"
	if ``dv'_p'>= 2.576 local `dv'_star = "***"
}

local runlist nsc_enr_pa_by_ nsc_asscplus_pa_by_ nsc_bachplus_pa_by_ any_pgib_use_by
local loop = 3
foreach dv in `runlist' {
	local loop = `loop' + 1 
	
	if "`dv'" == "nsc_enroll_by_" local dvlab "Enrolled"
	if "`dv'" == "nsc_asscplus_by_" local dvlab "Associates Deg+"
	if "`dv'" == "nsc_bachplus_by_" local dvlab "Bachelors Deg+"
	if "`dv'" == "nsc_enr_pa_by_" local dvlab "Enrolled (Post-Arrival)"
	if "`dv'" == "nsc_asscplus_pa_by_" local dvlab "Associates Deg+ (Post-Arrival)"
	if "`dv'" == "nsc_bachplus_pa_by_" local dvlab "Bachelors Deg+ (Post-Arrival)"
	if "`dv'" == "any_pgib_use_by" local dvlab "Any Post 9/11 GI Bill Use"
	
	cap file close b
	
		file open b using "${outputfolder}/TableA10_row`loop'.tex", write replace
		file write b "`dvlab'" _char(38) %5.`dec'f (``dv'2017_b') ("``dv'2017_star'") _char(38) %5.`dec'f (``dv'2017_m') _char(38) %5.`dec'f (``dv'2020_b') ("``dv'2020_star'") _char(38) %5.`dec'f (``dv'2020_m') _char(92) _char(92) _n
		file write b "" _char(38) _char(40) "``dv'2017_se'" _char(41) _char(38) _char(38) _char(40) "``dv'2020_se'" _char(41) _char(92) _char(92) _n
		file write b "Observations" _char(38) _char(38) "``dv'2017_N'" _char(38) "``dv'2020_N'"
		file close b


}
}


*************
* Table A11  
*************
	
	merge 1:1 pid using raw_dta/promo_mos_outcomes_analysis_samp.dta, keep(1 3) gen(merge_promo)
	merge 1:1 pid using raw_dta/mil_pay_outcomes_analysis_samp.dta,  keep(1 3) gen(merge_pay)
	
	foreach var in promo_e5_by_ diff_mos_by_ change_com_to_com_by_ change_non_to_non_by_ change_non_to_com_by_ change_com_to_non_by_   {
		forv x = 4(4)32 {
			replace `var'`x' = `var'`x' * 100
		}
	}
	
	global runlist  promo_e5_by_ ap_y_tot_comp_by_ diff_mos_by_ sep_by_ 
		
	qui { 
	eststo clear
	foreach fe in FE1 {  
	foreach dv in $runlist {
	foreach x in 8 12 16 { 
	if !("`x'" == "8" & "`dv'" == "promo_e6_by_") {
		eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x', absorb(${`fe'}) robust $ivregsettings
		
		
		local dec 2
		if "`dv'" == "ap_y_tot_comp_by_" local dec 0
		local `dv'`x'_b  = _b[${NEWENDOG}] * 10
		if "`dv'" == "ap_y_tot_comp_by_" local `dv'`x'_b : display %6.`dec'f ``dv'`x'_b'
		local `dv'`x'_se  = _se[${NEWENDOG}] * 10
		local `dv'`x'_se : display %5.`dec'f ``dv'`x'_se'
		if "`dv'" == "ap_y_tot_comp_by_" local `dv'`x'_se = substr("``dv'`x'_se'",strrpos("``dv'`x'_se'"," ")+1,.)

			reg `dv'`x' if e(sample)
			local `dv'`x'_m = _b[_cons]
			local `dv'`x'_m : display %5.`dec'f ``dv'`x'_m'
			if "`dv'" == "ap_y_tot_comp_by_" local `dv'`x'_m : display %6.`dec'f ``dv'`x'_m'

	local `dv'`x'_p = abs(``dv'`x'_b'/``dv'`x'_se')
	if ``dv'`x'_p' < 1.645 local `dv'`x'_star = ""
	if ``dv'`x'_p'>= 1.645 local `dv'`x'_star = "*"
	if ``dv'`x'_p'>= 1.96 local `dv'`x'_star = "**"
	if ``dv'`x'_p'>= 2.576 local `dv'`x'_star = "***"
	local n_`x' = `e(N)'
	}

	}
	}
	
	cap file close b
	file open b using "${outputfolder}/TableA11.tex", write replace

	foreach dv in $runlist {

if "`dv'" == "sep_by_" global axislab "Ever Separated"
if "`dv'" == "promo_e5_by_" global axislab "Promotion to Sergeant"	
if "`dv'" == "diff_mos_by_" global axislab "Changed Army Occupation"	
if "`dv'" == "ap_y_tot_comp_by_" global axislab "Cumulative Army Pay"

		local dec 2
		if "`dv'" == "ap_y_tot_comp_by_" local dec 0	
	if "`dv'" != "sep_by_" {
		file write b "${axislab}"  _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'12_b') ("``dv'12_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(92) _char(92) _n
		file write b ""  _char(38) _char(40)"``dv'8_se'" _char(41) _char(38) _char(40) "``dv'12_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(92) _char(92) _n
		file write b "Mean"  _char(38) "``dv'8_m'" _char(38) "``dv'12_m'" _char(38) "``dv'16_m'" _char(92) _char(92) _n
	}
	if "`dv'" == "sep_by_" {
		file write b "${axislab}" _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'12_b') ("``dv'12_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(92) _char(92) _n
		file write b "" _char(38) _char(40)"``dv'8_se'" _char(41) _char(38) _char(40) "``dv'12_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(92) _char(92) _n
		file write b "Mean" _char(38) "``dv'8_m'" _char(38) "``dv'12_m'" _char(38) "``dv'16_m'" _char(92) _char(92) _n
		file write b "\hline" _n
		file write b "Observations" _char(38) %5.0f (`n_8') _char(38) %5.0f (`n_12') _char(38) %5.0f (`n_16')  _char(92) _char(92) _n 
	}
	
}
		file close b

}
}




*************
* Table A12 
*************

	global runlist combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_ noncom_death_by_ ndi_despair2_by_
	
	qui { 
	local loop = 0 
	eststo clear
	foreach fe in FE1 {
	foreach dv in $runlist {
		local loop = `loop' + 1 
	foreach samp in " " "& init_afqsc < 50" "& init_afqsc >= 50" "& any_moral_waiver" "& !any_moral_waiver" "& init_black" "& init_hispanic" "& init_white" {
		 if "`samp'" == " " local grp = 1
		 if "`samp'" == "& init_afqsc < 50" local grp = 2
		 if "`samp'" == "& init_afqsc >= 50" local grp = 3
		 if "`samp'" == "& any_moral_waiver" local grp = 4
		 if "`samp'" == "& !any_moral_waiver" local grp = 5
		 if "`samp'" == "& init_black" local grp = 6
		 if "`samp'" == "& init_hispanic" local grp = 7
		 if "`samp'" == "& init_white" local grp = 8
	foreach x in  32 { 
	eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' `samp', absorb(${`fe'}) robust $ivregsettings
	local `dv'`x'`grp'_b = _b[${NEWENDOG}] * 10
	local `dv'`x'`grp'_se = _se[${NEWENDOG}] * 10
 
		reg `dv'`x' if e(sample)
		local `dv'`grp'_m = _b[_cons]
	
	local `dv'`x'`grp'_p = abs(``dv'`x'`grp'_b'/``dv'`x'`grp'_se')
	if ``dv'`x'`grp'_p' < 1.645 local `dv'`x'`grp'_star = ""
	if ``dv'`x'`grp'_p'>= 1.645 local `dv'`x'`grp'_star = "*"
	if ``dv'`x'`grp'_p'>= 1.96 local `dv'`x'`grp'_star = "**"
	if ``dv'`x'`grp'_p'>= 2.576 local `dv'`x'`grp'_star = "***"
	
	local dec 2

	local `dv'`x'`grp'_se: display %5.`dec'f ``dv'`x'`grp'_se'
	if ``dv'`x'`grp'_se' < 10 local `dv'`x'`grp'_se = substr("``dv'`x'`grp'_se'",-4,.)
	}
	}

	
if "`dv'" == "combat_death_by_" global axislab "Combat Death"
if "`dv'" == "wia_by_" global axislab "Ever Combat Injury"
if "`dv'" == "any_sigprofile_by_" global axislab "Significant Army Profile"
if "`dv'" == "amtvadc_in_" global axislab "Annual Amt VADC"
if "`dv'" == "anyvadc_in_" global axislab "Any VADC Receipt"
if "`dv'" == "noncom_death_by_" global axislab "Noncombat Death"
if "`dv'" == "ndi_despair2_by_" global axislab "Death of Despair"

	file open b using "${outputfolder}/TableA12_row`loop'.tex", write replace
	file write b "${axislab}" _char(38) %5.`dec'f (``dv'321_b') ("``dv'321_star'") _char(38) %5.`dec'f (``dv'322_b') ("``dv'322_star'") _char(38) %5.`dec'f (``dv'323_b') ("``dv'323_star'") _char(38) %5.`dec'f (``dv'324_b') ("``dv'324_star'") _char(38) %5.`dec'f (``dv'325_b') ("``dv'325_star'")  _char(38) %5.`dec'f (``dv'326_b') ("``dv'326_star'")  _char(38) %5.`dec'f (``dv'327_b') ("``dv'327_star'")  _char(38) %5.`dec'f (``dv'328_b') ("``dv'328_star'")  _char(92) _char(92) _n

	file write b "" _char(38) _char(40) "``dv'321_se'" _char(41) _char(38) _char(40) "``dv'322_se'" _char(41) _char(38) _char(40) "``dv'323_se'" _char(41) _char(38) _char(40) "``dv'324_se'" _char(41) _char(38) _char(40) "``dv'325_se'" _char(41) _char(38) _char(40) "``dv'326_se'" _char(41) _char(38) _char(40) "``dv'327_se'" _char(41) _char(38) _char(40) "``dv'328_se'" _char(41) _char(92) _char(92) _n
	

	file write b "${axislab} Mean" _char(38) %5.`dec'f (``dv'1_m') _char(38) %5.`dec'f (``dv'2_m') _char(38) %5.`dec'f (``dv'3_m') _char(38) %5.`dec'f (``dv'4_m') _char(38) %5.`dec'f (``dv'5_m') _char(38) %5.`dec'f (``dv'6_m') _char(38) %5.`dec'f (``dv'7_m') _char(38) %5.`dec'f (``dv'8_m') _char(92) _char(92) _n

file close b

}	
}
}


*************
* Table A13
*************

 cap g nsc_ascpl_pstr_by_32 = nsc_asscplus_postarr_by_32
	forvalues y = 1(1)8 {
		local z = `y'*12 - 1 
egen mths_dep_in_`y'yr = rowtotal(deployed_0-deployed_`z')
bys dmsl_tc_first qtr : egen meandep = mean(mths_dep_in_`y'yr)  
bys dmsl_tc_first qtr : egen ct = total(1)
g instrument_mths_dep_`y'yr = ((meandep*ct) - mths_dep_in_`y'yr )/(ct-1) 
drop meandep ct
	}

assert mths_dep_in_3yr == mths_dep_win_3yr
assert instrument_mths_dep_3yr == instrument_mths_dep_qtr


global RUNLIST combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_ noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ bar_or_misconduct_by_ inc_combined_by_ vantage_v4_score731 nsc_enr_postarr_by_ nsc_ascpl_pstr_by_

local loop = 0 
qui {
foreach dv in $RUNLIST{
local loop = `loop' + 1 
	eststo clear 

		forvalues y = 3(1)8 {
	g ENDOG = mths_dep_in_`y'yr
	local INST instrument_mths_dep_`y'yr
	foreach x in 32 {
		if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
		eststo y`y':	ivreghdfe `dv'`x' (ENDOG = `INST') if ${rest} >= `x', absorb($FE1) robust
		}
		if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
		eststo y`y':	ivreghdfe `dv' (ENDOG = `INST') if ${rest} >= `x', absorb($FE1) robust
		}
	drop ENDOG 
	}
}

if "`dv'" == "combat_death_by_" global axislab "Combat Death"
if "`dv'" == "wia_by_" global axislab "Combat Injury"
if "`dv'" == "any_sigprofile_by_" global axislab "Significant Army Profile"
if "`dv'" == "amtvadc_in_" global axislab "Annual Amt VADC"
if "`dv'" == "anyvadc_in_" global axislab "Any VADC Receipt"
if "`dv'" == "noncom_death_by_" global axislab "Noncombat Death"
if "`dv'" == "ndi_despair2_by_" global axislab "Death of Despair"
if "`dv'" == "ndi_suicide_plus2_by_" global axislab "Suicide"
if "`dv'" == "ndi_drug2_by_" global axislab "Drug- or Alcohol-Rel. Death"
if "`dv'" == "ndi_mv_acc2_by_" global axislab "Motor Vehicle Death"
if "`dv'" == "bar_or_misconduct_by_" global axislab "Separated for Misconduct"
if "`dv'" == "inc_combined_by_" global axislab "Ever Incarcerated"
if "`dv'" == "vantage_v4_score731" global axislab "Credit Score in 2020 (Vantage)"
if "`dv'" == "nsc_enr_postarr_by_" global axislab "College Enrollment"
if "`dv'" == "nsc_ascpl_pstr_by_" global axislab "Associate's Deg+"


esttab y*  using ${outputfolder}\TableA13_row`loop'.tex ///
	, ///
	cells(b(fmt(2) star) se(par fmt(2)))  ///
	transform( @*${SCALE} ${SCALE}) ///
	replace nonumber noobs nomtitle nodepvar nonumber plain fragment label ///
	substitute(_ \_) style(tex) ///
	collabels(, none) mlabels(, none)  ///
	keep(ENDOG, relax) ///
	varlabels(ENDOG "${axislab}") ///
	starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30) ///
	stats()

}
}


*************
* Table A14   
*************

use ".\created_dta/${analysissampwomen}", clear

	global runlist combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_ noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ bar_or_misconduct_by_ inc_combined_by_  vantage_v4_score731 nsc_enr_pa_by_2020 nsc_asscplus_pa_by_2020
	local panel_a combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_
	local panel_b noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_
	local panel_c bar_or_misconduct_by_ inc_combined_by_  vantage_v4_score731 nsc_enr_pa_by_2020 nsc_asscplus_pa_by_2020
	
		
	*qui { 
	eststo clear
	foreach fe in FE1 { 
	foreach dv in $runlist {
	global timelist 8 16 24 32
	foreach x in ${timelist} { 
	
	if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
		eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb(${`fe'}) robust $ivregsettings
		local `dv'`x'_n = `e(N)'
	}
	if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
		eststo `dv'_`x' : ivreghdfe `dv' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb(${`fe'}) robust $ivregsettings
		local `dv'`x'_n = `e(N)'
	}
	
	
	local dec 2
	if inlist("`dv'","amtdisability_in_","amtvadc_in_") local dec 2	
	
	local `dv'`x'_b = _b[${NEWENDOG}] * 10
	local `dv'`x'_se = _se[${NEWENDOG}] * 10
	
	local `dv'`x'_p = abs(``dv'`x'_b'/``dv'`x'_se')
	if ``dv'`x'_p' < 1.645 local `dv'`x'_star = ""
	if ``dv'`x'_p'>= 1.645 local `dv'`x'_star = "*"
	if ``dv'`x'_p'>= 1.96 local `dv'`x'_star = "**"
	if ``dv'`x'_p'>= 2.576 local `dv'`x'_star = "***"
	
	local `dv'`x'_se: display %5.`dec'f ``dv'`x'_se'
	if ``dv'`x'_se' < 10 & `dec' == 2 local `dv'`x'_se = substr("``dv'`x'_se'",-4,.)
	if `x' == 32 { 
		if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
			reg `dv'`x' if e(sample)
		}
		if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
			reg `dv' if e(sample)
		}
		local `dv'_m = _b[_cons]
	}

	}
	
if "`dv'" == "combat_death_by_" global `dv'_axislab "Combat Death"
if "`dv'" == "wia_by_" global `dv'_axislab "Ever Combat Injury"
if "`dv'" == "any_sigprofile_by_" global `dv'_axislab "Significant Army Profile"
if "`dv'" == "amtvadc_in_" global `dv'_axislab "Annual Amt VADC"
if "`dv'" == "anyvadc_in_" global `dv'_axislab "Any VADC Receipt"
if "`dv'" == "noncom_death_by_" global `dv'_axislab "Noncombat Death"
if "`dv'" == "ndi_despair2_by_" global `dv'_axislab "Death of Despair"
if "`dv'" == "ndi_suicide_plus2_by_" global `dv'_axislab "Suicide"
if "`dv'" == "ndi_drug2_by_" global `dv'_axislab "Drug- or Alcohol-Rel. Death"
if "`dv'" == "ndi_mv_acc2_by_" global `dv'_axislab "Motor Vehicle Death"
if "`dv'" == "bar_or_misconduct_by_" global `dv'_axislab "Separated for Misconduct/Barred"
if "`dv'" == "inc_combined_by_" global `dv'_axislab "Ever Incarcerated"
if "`dv'" == "vantage_v4_score731" global `dv'_axislab "Credit Score in 2020 (Vantage)"
if "`dv'" == "nsc_enr_pa_by_2020" global `dv'_axislab "Enrolled by 2020 (Post-Arrival)"
if "`dv'" == "nsc_asscplus_pa_by_2020" global `dv'_axislab "Assc Deg+ by 2020 (Post-Arrival)"
	}
}
	foreach typ in a b c {
			file open b using "${outputfolder}/TableA14_panel`typ'.tex", write replace
		foreach dv in `panel_`typ'' {
			if !inlist("`dv'","nsc_asscplus_pa_by_2020"){
				file write b "${`dv'_axislab}" _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(38) %5.`dec'f (``dv'24_b') ("``dv'24_star'") _char(38) %5.`dec'f (``dv'32_b') ("``dv'32_star'") _char(38) %5.`dec'f (``dv'_m') _char(92) _char(92) _n
				file write b "" _char(38) _char(40) "``dv'8_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(38) _char(40) "``dv'24_se'" _char(41) _char(38) _char(40) "``dv'32_se'" _char(41) _char(38) _char(92) _char(92) _char(92) _char(92) _n
			}
			
			if inlist("`dv'","nsc_asscplus_pa_by_2020"){
				file write b "${`dv'_axislab}"  _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(38) %5.`dec'f (``dv'24_b') ("``dv'24_star'") _char(38) %5.`dec'f (``dv'32_b') ("``dv'32_star'") _char(38) %5.`dec'f (``dv'_m') _char(92) _char(92) _n
				file write b "" _char(38) _char(40) "``dv'8_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(38) _char(40) "``dv'24_se'" _char(41) _char(38) _char(40) "``dv'32_se'" _char(41) _char(38) _char(92) _char(92) _n
				file write b "\hline"  _char(92) _char(92)  _n
				file write b "Observations" _char(38) "``dv'8_n'" _char(38) "``dv'16_n'" _char(38) "``dv'24_n'" _char(38) "``dv'32_n'" _char(38) "``dv'32_n'" _n
			}
		
		}
			file close b
	}



use "created_dta/${analysissamp}", clear

forv i = 1/36 {
	gen amtssissdi_in_`i' = amtdisability_in_`i' - amtvadc_in_`i'
}	


*************
* Table A15  
*************
	global runlist combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_ noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ bar_or_misconduct_by_ inc_combined_by_ nsc_enr_postarr_by_ nsc_asscplus_postarr_by_  
	qui { 
	eststo clear
	local loop = 0 
	foreach fe in FE1_qtr { 
	foreach dv in $runlist {
		local loop = `loop' + 1 
	foreach x in 8 16 24 32 { 
	eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb(${`fe'}) robust $ivregsettings
	local `dv'`x'_b = _b[${NEWENDOG}] * 10
	local `dv'`x'_se = _se[${NEWENDOG}] * 10
	
	local dec 2
	local `dv'`x'_se: display %5.`dec'f ``dv'`x'_se'
	if ``dv'`x'_se' < 10 & `dec' == 2 local `dv'`x'_se = substr("``dv'`x'_se'",-4,.)
	if `x' == 32 { 
		reg `dv'`x' if e(sample)
		local `dv'_m = _b[_cons]
	}
	local `dv'`x'_p = abs(``dv'`x'_b'/``dv'`x'_se')
	if ``dv'`x'_p' < 1.645 local `dv'`x'_star = ""
	if ``dv'`x'_p'>= 1.645 local `dv'`x'_star = "*"
	if ``dv'`x'_p'>= 1.96 local `dv'`x'_star = "**"
	if ``dv'`x'_p'>= 2.576 local `dv'`x'_star = "***"
	
	}

if "`dv'" == "combat_death_by_" global axislab "Combat Death"
if "`dv'" == "wia_by_" global axislab "Ever Combat Injury"
if "`dv'" == "any_sigprofile_by_" global axislab "Significant Army Profile"
if "`dv'" == "amtvadc_in_" global axislab "Annual Amt VADC"
if "`dv'" == "anyvadc_in_" global axislab "Any VADC Receipt"
if "`dv'" == "noncom_death_by_" global axislab "Noncombat Death"
if "`dv'" == "ndi_despair2_by_" global axislab "Death of Despair"
if "`dv'" == "ndi_suicide_plus2_by_" global axislab "Suicide"
if "`dv'" == "ndi_drug2_by_" global axislab "Drug- or Alcohol-Rel. Death"
if "`dv'" == "ndi_mv_acc2_by_" global axislab "Motor Vehicle Death"
if "`dv'" == "bar_or_misconduct_by_" global axislab "Separated for Misconduct/Barred"
if "`dv'" == "inc_combined_by_" global axislab "Ever Incarcerated"
if "`dv'" == "vantage_v4_score731" global axislab "Credit Score in 2020 (Vantage)"
if "`dv'" == "nsc_enr_postarr_by_" global axislab "Enroll Post-arrival"
if "`dv'" == "nsc_asscplus_postarr_by_" global axislab "Assc Deg+ Post-arrival"
	
	file open b using "${outputfolder}/TableA15_row`loop'.tex", write replace
	if !inlist("`dv'","nsc_asscplus_postarr_by_") {
		file write b "${axislab}" _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(38) %5.`dec'f (``dv'24_b') ("``dv'24_star'") _char(38) %5.`dec'f (``dv'32_b') ("``dv'32_star'") _char(38) %5.`dec'f (``dv'_m') _char(92) _char(92) _n
file write b "" _char(38) _char(40) "``dv'8_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(38) _char(40) "``dv'24_se'" _char(41) _char(38) _char(40) "``dv'32_se'" _char(41) _char(38) _char(92) _char(92) _n
		file close b
	}
	if inlist("`dv'","nsc_asscplus_postarr_by_") {
		file write b "${axislab}" _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(38) %5.`dec'f (``dv'24_b') ("``dv'24_star'") _char(38) %5.`dec'f (``dv'32_b') ("``dv'32_star'") _char(38) %5.`dec'f (``dv'_m') _char(92) _char(92) _n
file write b "" _char(38) _char(40) "``dv'8_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(38) _char(40) "``dv'24_se'" _char(41) _char(38) _char(40) "``dv'32_se'" _char(41) _char(38) _char(92) _char(92) "\hline" _n
		file write b "Observations" _char(38) "157,415" _char(38) "157,415" _char(38) "129,176" _char(38) "101,387" _char(38) "101,387" _n
		file close b
	}
}
		
}
	}




*************
* Table A16 
*************

preserve 

g ${NEWINST}2 = ${NEWINST}^2 
g ${NEWENDOG}2 = ${NEWENDOG}^2
g ${NEWINST}3 = ${NEWINST}^3
g ${NEWENDOG}3 = ${NEWENDOG}^3
g ${NEWENDOG}_c2 = ${NEWENDOG}_casnotsd^2
g ${NEWINST}_c2 = ${NEWINST}_casnotsd^2
g ${NEWENDOG}_c3 = ${NEWENDOG}_casnotsd^3
g ${NEWINST}_c3 = ${NEWINST}_casnotsd^3
global RUNLIST   amtvadc_in_ anyvadc_in_ noncom_death_by_


qui{
foreach dv in $RUNLIST {
	eststo clear
foreach x in 32 { 
	
eststo `dv'_`x'_1 : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
	lincom 10*${NEWENDOG}
	estadd scalar rest = r(estimate)  
	estadd scalar rse = r(se) 
	
eststo `dv'_`x'_2 : ivreghdfe `dv'`x' (${NEWENDOG} ${NEWENDOG}2 = ${NEWINST} ${NEWINST}2) if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
	lincom 10*${NEWENDOG}+(100)*${NEWENDOG}2
	estadd scalar rest = r(estimate)  
	estadd scalar rse = r(se) 

eststo `dv'_`x'_3 : ivreghdfe `dv'`x' (${NEWENDOG} ${NEWENDOG}2 ${NEWENDOG}3 = ${NEWINST} ${NEWINST}2 ${NEWINST}3) if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
	lincom 10*${NEWENDOG}+(100)*${NEWENDOG}2+1000*${NEWENDOG}3
	estadd scalar rest = r(estimate)  
	estadd scalar rse = r(se) 

eststo `dv'_`x'_4 : ivreghdfe `dv'`x' (dep_win_3yr  ${NEWENDOG} = ${ANYDEPINST} ${NEWINST} ) if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
	lincom 10*${NEWENDOG} + dep_win_3yr
	estadd scalar rest = r(estimate)  
	estadd scalar rse = r(se) 

eststo `dv'_`x'_5 : ivreghdfe `dv'`x' (dep_win_3yr  ${NEWENDOG}  ${NEWENDOG}2 = ${ANYDEPINST} ${NEWINST}  ${NEWINST}2) if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
	lincom 10*${NEWENDOG} +(100)*${NEWENDOG}2 + dep_win_3yr
	estadd scalar rest = r(estimate)  
	estadd scalar rse = r(se) 

eststo `dv'_`x'_6 : ivreghdfe `dv'`x' (dep_win_3yr  ${NEWENDOG} ${NEWENDOG}2   ${NEWENDOG}3 = ${ANYDEPINST} ${NEWINST} ${NEWINST}2 ${NEWINST}3) if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
	lincom 10*${NEWENDOG} +(100)*${NEWENDOG}2 + 1000*${NEWENDOG}3 + dep_win_3yr
	estadd scalar rest = r(estimate)  
	estadd scalar rse = r(se) 

}
	
	local dec 3
	if "`dv'" == "anyvadc_in_" local pan a
	if "`dv'" == "anyvadc_in_" local decim 3
	if "`dv'" == "amtvadc_in_" local pan b
	if "`dv'" == "amtvadc_in_" local decim 0
	if "`dv'" == "noncom_death_by_" local pan c
	if "`dv'" == "noncom_death_by_" local decim 3
 
	noi esttab  `dv'*  using "${outputfolder}\TableA16_panel`pan'.tex"  ///
	, ///
	cells(b(fmt(`dec') star) se(par fmt(`dec')))  ///
	replace noobs nomtitle nodepvar nonumber plain fragment label ///
	substitute(_ \_) style(tex) ///
	mlabels(, none) collabels(, none) ///
	keep(${NEWENDOG} dep_win_3yr ${NEWENDOG}2 ${NEWENDOG}3  ${NEWENDOG}_casnotsd ${NEWENDOG}_c2 ${NEWENDOG}_c3, relax) ///
	varlabels(${NEWENDOG} "Months Deployed" dep_win_3yr "Deployed"  ${NEWENDOG}2  "Months Dep. Sq." ${NEWENDOG}3 "Months Dep. Cu."  ${NEWENDOG}_casnotsd "Months Dep. X Peer Cas." ${NEWENDOG}_c2 "Months Dep. X Peer Cas. Sq." ${NEWENDOG}_c3 "Months Dep. X Peer Cas. Cu.") wrap ///
	starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30) ///
	stats(rest rse, labels("\hline \\ Effect of 0 to 10 Months Dep" "") fmt(%9.`decim'f %9.`decim'f))
	
}
}
restore 



*************
* Table A17
*************


use "created_dta/${analysissamp}", clear

forv i = 1/36 {
	gen amtssissdi_in_`i' = amtdisability_in_`i' - amtvadc_in_`i'
}	

cap g nsc_aplus_by_32 = nsc_asscplus_postarr_by_32 
global RUNLIST combat_death_by_  wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_ noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ bar_or_misconduct_by_ inc_combined_by_ vantage_v4_score731 nsc_enr_postarr_by_ nsc_aplus_by_  

local panel_a combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_
local panel_b noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_
local panel_c bar_or_misconduct_by_ inc_combined_by_ vantage_v4_score731 nsc_enr_postarr_by_ nsc_aplus_by_  

eststo clear 
foreach dv in $RUNLIST {
	foreach x in 32 { 
		if "`dv'" != "vantage_v4_score731" {
			eststo `dv'`x'_1 : qui ivreghdfe `dv'`x' (dep_win_3yr  = ${ANYDEPINST}) if ${rest} >= `x', absorb($FE1) robust $ivregsettings
				local `dv'`x'_b_1 = _b[dep_win_3yr]
				local `dv'`x'_se_1 = _se[dep_win_3yr]
				local `dv'`x'_n_1 = `e(N)'
			eststo `dv'`x'_2 : qui ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x', absorb($FE1) robust $ivregsettings
				local `dv'`x'_b_2 = _b[${NEWENDOG}] * 10
				local `dv'`x'_se_2 = _se[${NEWENDOG}] * 10
				local `dv'`x'_n_2 = `e(N)'
		}
		if "`dv'" == "vantage_v4_score731" {
			eststo `dv'`x'_1 : qui ivreghdfe `dv' (dep_win_3yr  = ${ANYDEPINST}) if ${rest} >= `x', absorb($FE1) robust $ivregsettings
				local `dv'`x'_b_1 = _b[dep_win_3yr]
				local `dv'`x'_se_1 = _se[dep_win_3yr]
				local `dv'`x'_n_1 = `e(N)'
			eststo `dv'`x'_2 : qui ivreghdfe `dv' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x', absorb($FE1) robust $ivregsettings
				local `dv'`x'_b_2 = _b[${NEWENDOG}] * 10
				local `dv'`x'_se_2 = _se[${NEWENDOG}] * 10
				local `dv'`x'_n_2 = `e(N)'
		} 
	
	
	local dec 2
	if inlist("`dv'","amtdisability_in_","amtvadc_in_") local dec 2	
	
	forv i = 1/2{
		local `dv'`x'_p_`i' = abs(``dv'`x'_b_`i''/``dv'`x'_se_`i'')
		if ``dv'`x'_p_`i'' < 1.645 local `dv'`x'_star_`i' = ""
		if ``dv'`x'_p_`i''>= 1.645 local `dv'`x'_star_`i' = "*"
		if ``dv'`x'_p_`i''>= 1.96 local `dv'`x'_star_`i' = "**"
		if ``dv'`x'_p_`i''>= 2.576 local `dv'`x'_star_`i' = "***"
		
		local `dv'`x'_se_`i': display %5.`dec'f ``dv'`x'_se_`i''
		if ``dv'`x'_se_`i'' < 10 & `dec' == 2 local `dv'`x'_se_`i' = substr("``dv'`x'_se_`i''",-4,.)
	}
	
	}
	
if "`dv'" == "combat_death_by_" global `dv'_axislab "Combat Death"
if "`dv'" == "wia_by_" global `dv'_axislab "Combat Injury"
if "`dv'" == "any_sigprofile_by_" global `dv'_axislab "Sig. Army Profile"
if "`dv'" == "amtvadc_in_" global `dv'_axislab "Amt VADC"
if "`dv'" == "anyvadc_in_" global `dv'_axislab "Any VADC"
if "`dv'" == "noncom_death_by_" global `dv'_axislab "Noncombat Death"
if "`dv'" == "ndi_despair2_by_" global `dv'_axislab "Death of Despair"
if "`dv'" == "ndi_suicide_plus2_by_" global `dv'_axislab "Suicide"
if "`dv'" == "ndi_drug2_by_" global `dv'_axislab "Drug- or Alcohol-Rel Death"
if "`dv'" == "ndi_mv_acc2_by_" global `dv'_axislab "Motor Vehicle Death"
if "`dv'" == "bar_or_misconduct_by_" global `dv'_axislab "Separated for Misconduct"
if "`dv'" == "inc_combined_by_" global `dv'_axislab "Ever Incarcerated"
if "`dv'" == "vantage_v4_score731" global `dv'_axislab "Credit Score"
if "`dv'" == "nsc_enr_postarr_by_" global `dv'_axislab "College Enrollment"
if "`dv'" == "nsc_aplus_by_" global `dv'_axislab "Associates Deg+"
}


foreach typ in a b c {
			file open b using "${outputfolder}/TableA17_panel`typ'.tex", write replace
		foreach dv in `panel_`typ'' {
			if !inlist("`dv'","nsc_aplus_by_"){
				file write b "${`dv'_axislab}" _char(38) %5.`dec'f (``dv'32_b_1') ("``dv'32_star_1'") _char(38) %5.`dec'f (``dv'32_b_2') ("``dv'32_star_2'") _char(38) _char(92) _char(92) _n
				file write b "" _char(38) _char(40) "``dv'32_se_1'" _char(41) _char(38) _char(40) "``dv'32_se_2'" _char(41) _char(38) _char(92) _char(92) _char(92) _n
			}
			
			if inlist("`dv'","nsc_aplus_by_"){
				file write b "${`dv'_axislab}" _char(38) %5.`dec'f (``dv'32_b_1') ("``dv'32_star_1'") _char(38) %5.`dec'f (``dv'32_b_2') ("``dv'32_star_2'") _char(38) _char(92) _char(92) _n
				file write b "" _char(38) _char(40) "``dv'32_se_1'" _char(41) _char(38) _char(40) "``dv'32_se_2'" _char(41) _char(38) _char(92) _char(92) _char(92) _n
				file write b "\hline"  _char(92) _char(92)  _n
				file write b "Observations" _char(38) "``dv'32_n_1'" _char(38) "``dv'32_n_2'" _char(38) _n
			}
		}
			file close b
}



*************
* Table A18  
*************

global RUNLIST black hisp other_race married init_nrdep hsgplus init_approx_age init_afqsc

egen tempbctxQFE = group(dmsl_tc_first qtr)
levelsof tempbctxQFE, local(l)
qui sum tempbctxQFE, d 
local maxer = `r(max)'
forv x = 1/`maxer' {
	gen bctXQFE`x' = tempbctxQFE == `x'
}

eststo clear 
foreach dv in $RUNLIST{
		global FE1_temp $FE1
		if "`dv'" == "init_afqsc" global FE1_temp afqs_mi mos_tc_post_yr_terms
		
		ivreghdfe `dv' (${NEWENDOG} = ${NEWINST}), absorb($FE1_temp) robust $ivregsettings
		
		summ casnotsd if e(sample) == 1 
			local `dv'_b1 = _b[${NEWENDOG}] * 10
			local `dv'_se1 = _se[${NEWENDOG}] * 10
			local `dv'n_baseln = `r(N)'
			
		
		manyiv `dv' (${NEWENDOG} = bctXQFE*), absorb(${FE1_temp}) forcejive
		
		count if e(sample)
			local `dv'_b2sls = e(b)[1,2] * 10
			local `dv'_se2sls = e(se)[2,2] * 10
			local `dv'_bujive = e(b)[1,6] * 10
			local `dv'_seujive = e(se)[2,6] *10
			local `dv'_bliml = e(b)[1,3] *10
			local `dv'_seliml = e(se)[2,3] *10
			local `dv'n_manyiv = `r(N)'
		
		local dec 2
		if inlist("`dv'","black","hisp","other_race","married","anydepc","hsgplus") {
			foreach i in 1 ujive liml 2sls {
				local `dv'_b`i' = ``dv'_b`i'' *100
				local `dv'_se`i' = ``dv'_se`i'' *100
			}
		}
		
		foreach i in 1 ujive liml 2sls {
		local `dv'_se`i': display %5.`dec'f ``dv'_se`i''
		if ``dv'_se`i'' < 10 & `dec' == 2 local `dv'_se`i' = substr("``dv'_se`i''",-4,.)
		if `dec' == 0 local `dv'_se`i' = substr("``dv'_se`i''",strrpos("``dv'_se`i''"," ")+1,.)
		}
	
		foreach i in 1 ujive liml 2sls {
		local `dv'_p`i' = abs(``dv'_b`i''/``dv'_se`i'')
		if ``dv'_p`i'' < 1.645 local `dv'_st`i' = ""
		if ``dv'_p`i''>= 1.645 local `dv'_st`i' = "*"
		if ``dv'_p`i''>= 1.96 local `dv'_st`i' = "**"
		if ``dv'_p`i''>= 2.576 local `dv'_st`i' = "***"
		}

	

if "`dv'" == "black" global `dv'axislab "Black"
if "`dv'" == "hisp" global `dv'axislab "Hispanic"
if "`dv'" == "other_race" global `dv'axislab "Other Race"
if "`dv'" == "married" global `dv'axislab "Married"
if "`dv'" == "init_nrdep" global `dv'axislab "Num. Dependents"
if "`dv'" == "hsgplus" global `dv'axislab "HS Graduate +"
if "`dv'" == "init_approx_age" global `dv'axislab "Age"
if "`dv'" == "init_afqsc" global `dv'axislab "AFQT"
}


	file open b using "${outputfolder}/TableA18.tex", write replace
	foreach dv in $RUNLIST{
		
		if !inlist("`dv'","init_afqsc") {
		file write b "${`dv'axislab}" _char(38) %6.`dec'f (``dv'_b1') ("``dv'_st1'") _char(38) %6.`dec'f (``dv'_bujive') ("``dv'_stujive'") _char(38) %6.`dec'f (``dv'_bliml') ("``dv'_stliml'") _char(38) %6.`dec'f (``dv'_b2sls') ("``dv'_st2sls'") _char(92) _char(92) _n
		file write b "" _char(38) _char(40) "``dv'_se1'" _char(41) _char(38) _char(40) "``dv'_seujive'" _char(41) _char(38) _char(40) "``dv'_seliml'" _char(41) _char(38) _char(40) "``dv'_se2sls'" _char(41) _char(92) _char(92) _n
		}
		if inlist("`dv'","init_afqsc") {
			file write b "${`dv'axislab}" _char(38) %6.`dec'f (``dv'_b1') ("``dv'_st1'") _char(38) %6.`dec'f (``dv'_bujive') ("``dv'_stujive'") _char(38) %6.`dec'f (``dv'_bliml') ("``dv'_stliml'") _char(38) %6.`dec'f (``dv'_b2sls') ("``dv'_st2sls'")  _char(92) _char(92) _n
			file write b "" _char(38) _char(40) "``dv'_se1'" _char(41) _char(38) _char(40) "``dv'_seujive'" _char(41) _char(38) _char(40) "``dv'_seliml'" _char(41) _char(38) _char(40) "``dv'_se2sls'" _char(41) _char(92) _char(92) _n
			file write b ("\hline") _char(92) _char(92) _n
			file write b ("[-1.0em] ") "Obs." _char(38) %6.0fc (``dv'n_baseln') _char(38) %6.0fc (``dv'n_manyiv') _char(38) %6.0fc (``dv'n_manyiv') _char(38) %6.0fc (``dv'n_manyiv') _char(92) _char(92) _n	
		}
		
	}
	file close b
	

*************
* Table A19
*************

cap g nsc_ascpl_pstr_by_32 = nsc_asscplus_postarr_by_32

global RUNLIST combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_ noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ bar_or_misconduct_by_ inc_combined_by_ vantage_v4_score731 nsc_enr_postarr_by_ nsc_ascpl_pstr_by_

qui{
cap egen tempbctxQFE = group(dmsl_tc_first qtr)
levelsof tempbctxQFE, local(l)
qui sum tempbctxQFE, d 
local maxer = `r(max)'
forv x = 1/`maxer' {
	cap gen bctXQFE`x' = tempbctxQFE == `x'
}

eststo clear 
local loop = 0 
foreach dv in $RUNLIST{
	local loop = `loop' + 1 
	
	foreach x in 32 {
		if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
			ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x', absorb($FE1) robust $ivregsettings
		}
		if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
			ivreghdfe `dv' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x', absorb($FE1) robust $ivregsettings
		}
		summ casnotsd if e(sample) == 1 
			local `dv'`x'_b1 = _b[${NEWENDOG}] * 10
			local `dv'`x'_se1 = _se[${NEWENDOG}] * 10
			local `dv'n_baseln = `r(N)'
			
		
		if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
			manyiv `dv'`x' (${NEWENDOG} = bctXQFE*) if ${rest} >= `x', absorb(${FE1}) forcejive
		}
		if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
			manyiv `dv' (${NEWENDOG} = bctXQFE*) if ${rest} >= `x', absorb(${FE1}) forcejive
		}
		count if e(sample)
			local `dv'`x'_b2sls = e(b)[1,2] * 10
			local `dv'`x'_se2sls = e(se)[2,2] * 10
			local `dv'`x'_bujive = e(b)[1,6] * 10
			local `dv'`x'_seujive = e(se)[2,6] *10
			local `dv'`x'_bliml = e(b)[1,3] *10
			local `dv'`x'_seliml = e(se)[2,3] *10
			local `dv'n_manyiv = `r(N)'
		
		local dec 2
			if inlist("`dv'","ndi_mv_acc2_by_", "noncom_death_by_", "ndi_despair2_by_") local dec 3
			if inlist("`dv'", "amtvadc_in_") local dec 0 

		foreach i in 1 ujive liml 2sls {
		local `dv'`x'_se`i': display %5.`dec'f ``dv'`x'_se`i''
		if ``dv'`x'_se`i'' < 10 & `dec' == 2 local `dv'`x'_se`i' = substr("``dv'`x'_se`i''",-4,.)
		if `dec' == 0 local `dv'`x'_se`i' = substr("``dv'`x'_se`i''",strrpos("``dv'`x'_se`i''"," ")+1,.)
		}
	
		foreach i in 1 ujive liml 2sls {
		local `dv'`x'_p`i' = abs(``dv'`x'_b`i''/``dv'`x'_se`i'')
		if ``dv'`x'_p`i'' < 1.645 local `dv'`x'_st`i' = ""
		if ``dv'`x'_p`i''>= 1.645 local `dv'`x'_st`i' = "*"
		if ``dv'`x'_p`i''>= 1.96 local `dv'`x'_st`i' = "**"
		if ``dv'`x'_p`i''>= 2.576 local `dv'`x'_st`i' = "***"
		}

if "`dv'" == "combat_death_by_" global axislab "Combat Death"
if "`dv'" == "wia_by_" global axislab "Ever Combat Injury"
if "`dv'" == "any_sigprofile_by_" global axislab "Significant Army Profile"
if "`dv'" == "amtvadc_in_" global axislab "Annual Amt VADC"
if "`dv'" == "anyvadc_in_" global axislab "Any VADC Receipt"
if "`dv'" == "noncom_death_by_" global axislab "Noncombat Death"
if "`dv'" == "ndi_despair2_by_" global axislab "Death of Despair"
if "`dv'" == "ndi_suicide_plus2_by_" global axislab "Suicide"
if "`dv'" == "ndi_drug2_by_" global axislab "Drug- or Alcohol-Rel. Death"
if "`dv'" == "ndi_mv_acc2_by_" global axislab "Motor Vehicle Death"
if "`dv'" == "bar_or_misconduct_by_" global axislab "Separated for Misconduct/Barred"
if "`dv'" == "inc_combined_by_" global axislab "Ever Incarcerated"
if "`dv'" == "vantage_v4_score731" global axislab "Credit Score in 2020 (Vantage)"
if "`dv'" == "nsc_enr_postarr_by_" global axislab "Enroll Post-arrival"
if "`dv'" == "nsc_asscplus_postarr_by_" global axislab "Assc Deg+ Post-arrival"

		file open b using "${outputfolder}/TableA19_row`loop'.tex", write replace
		
		file write b "${axislab}" _char(38) %6.`dec'f (``dv'`x'_b1') ("``dv'`x'_st1'") _char(38) %6.`dec'f (``dv'`x'_bujive') ("``dv'`x'_stujive'") _char(38) %6.`dec'f (``dv'`x'_bliml') ("``dv'`x'_stliml'") _char(38) %6.`dec'f (``dv'`x'_b2sls') ("``dv'`x'_st2sls'") _char(92) _char(92) _n
		file write b "" _char(38) _char(40) "``dv'`x'_se1'" _char(41) _char(38) _char(40) "``dv'`x'_seujive'" _char(41) _char(38) _char(40) "``dv'`x'_seliml'" _char(41) _char(38) _char(40) "``dv'`x'_se2sls'" _char(41) _char(92) _char(92) _n
		file write b "Obs." _char(38) %6.0fc (``dv'n_baseln') _char(38) %6.0fc (``dv'n_manyiv') _char(38) %6.0fc (``dv'n_manyiv')  _char(38) %6.0fc (``dv'n_manyiv') _char(92) _char(92) _n

		file close b
		
	
	}
	

}
}	



*************
* Table A20
*************

preserve 

egen post_yr_terms=group(post_orgraa init_terms year_arrival)
egen mos_tc_post_terms=group(init_pmos_cons post_orgraa init_terms)
egen mos_tc_yr_terms=group(init_pmos_cons init_terms year_arrival)
egen mos_tc_post_yr=group(init_pmos_cons post_orgraa  year_arrival)
egen mos_tc_post_qtr=group(init_pmos_cons post_orgraa  qtr)


qui {   
foreach cascat in casnotsd   { 
	if "`cascat'" == "casnotsd" local caslab = ""

foreach INST in NEWINST { 

eststo clear
estimates clear
foreach SPEC in FE1 {
	
foreach cov of varlist black hisp other_race married init_nrdep hsgplus init_approx_age init_afqsc { 
areg `cov', a(${`SPEC'})
predict r`cov', resid
local dof = e(N) - e(k_absorb) - 1

areg ${`INST'}, a(${`SPEC'})
cap drop indinst1
predict indinst1, resid

areg ${`INST'}_`cascat', a(${`SPEC'})
cap drop indinst2
predict indinst2, resid


if "`cov'"!= "init_afqsc" reg r`cov' indinst1 indinst2, dof(`dof')
estimates store n`cov'
if "`cov'"== "init_afqsc"  reg r`cov'  indinst1 indinst2 afqs_mi, dof(`dof')
estimates store n`cov'

}

suest nblack nhisp nother_race nmarried ninit_nrdep nhsgplus ninit_approx_age ninit_afqsc, vce(robust)
test indinst1 = 0 

test [nblack_mean]indinst1 = [nhisp_mean]indinst1 = [nother_race_mean]indinst1 = [nmarried_mean]indinst1 = [ninit_nrdep_mean]indinst1 = [nhsgplus_mean]indinst1 = [ninit_approx_age_mean]indinst1 = [ninit_afqsc_mean]indinst1 = 0
local jointF_base = r(p)
test indinst2 = 0 
test [nblack_mean]indinst2 = [nhisp_mean]indinst2 = [nother_race_mean]indinst2 = [nmarried_mean]indinst2 = [ninit_nrdep_mean]indinst2 = [nhsgplus_mean]indinst2 = [ninit_approx_age_mean]indinst2 = [ninit_afqsc_mean]indinst2 = 0
local jointF_inter = r(p)
test indinst1 = indinst2 = 0
test [nblack_mean]indinst1 = [nhisp_mean]indinst1 = [nother_race_mean]indinst1 = [nmarried_mean]indinst1 = [ninit_nrdep_mean]indinst1 = [nhsgplus_mean]indinst1 = [ninit_approx_age_mean]indinst1 = [ninit_afqsc_mean]indinst1 = [nblack_mean]indinst2 = [nhisp_mean]indinst2 = [nother_race_mean]indinst2 = [nmarried_mean]indinst2 = [ninit_nrdep_mean]indinst2 = [nhsgplus_mean]indinst2 = [ninit_approx_age_mean]indinst2 = [ninit_afqsc_mean]indinst2 = 0
local jointF_combined = r(p)
foreach cov of varlist black hisp other_race married init_nrdep hsgplus init_approx_age init_afqsc { //female
drop r`cov'
}
drop indinst*


eststo clear


foreach cov of varlist black hisp other_race married init_nrdep hsgplus init_approx_age init_afqsc { 
if "`cov'"!= "init_afqsc"  eststo `cov'bal : reghdfe `cov'  ${`INST'} ${`INST'}_`cascat', absorb(${`SPEC'} ) $SEa
if "`cov'"== "init_afqsc"  eststo `cov'bal : reghdfe `cov'   ${`INST'} ${`INST'}_`cascat', absorb(afqs_mi ${`SPEC'}  ) $SEa
local n = e(N)
summ casnotsd if e(sample)

local `cov'_`INST'_b = _b[${`INST'}] * ${SCALE}
local `cov'_`INST'_se = _se[${`INST'}] * ${SCALE}

if inlist("`cov'","black","hisp","other_race","married","hsgplus") local `cov'_`INST'_se = _se[${`INST'}] * ${SCALE} * 100
if inlist("`cov'","black","hisp","other_race","married","hsgplus") local `cov'_`INST'_b = _b[${`INST'}] * ${SCALE} * 100

	local `cov'`INST'_p = abs(``cov'_`INST'_b'/``cov'_`INST'_se')
	
		if ``cov'`INST'_p' < 1.645 local `cov'`INST'_st = ""
		if ``cov'`INST'_p'>= 1.645 local `cov'`INST'_st = "*"
		if ``cov'`INST'_p'>= 1.960 local `cov'`INST'_st = "**"
		if ``cov'`INST'_p'>= 2.576 local `cov'`INST'_st = "***"
		
loc dec 2
local `cov'_`INST'_se: display %5.`dec'f ``cov'_`INST'_se'
if ``cov'_`INST'_se' < 10 & `dec' == 2 local `cov'_`INST'_se = substr("``cov'_`INST'_se'",-4,.)
		
	
local `cov'_`cascat'_b = _b[${`INST'}_`cascat'] * ${SCALE} * r(sd)
local `cov'_`cascat'_se = _se[${`INST'}_`cascat'] * ${SCALE} * r(sd)


if inlist("`cov'","black","hisp","other_race","married","hsgplus") local `cov'_`cascat'_se = _se[${`INST'}_`cascat'] * ${SCALE} * r(sd) * 100
if inlist("`cov'","black","hisp","other_race","married","hsgplus") local `cov'_`cascat'_b =_b[${`INST'}_`cascat'] * ${SCALE} * r(sd) * 100

		local `cov'`cascat'_p = abs(``cov'_`cascat'_b'/``cov'_`cascat'_se')
		
		
		if ``cov'`cascat'_p' < 1.645 local `cov'`cascat'_st = ""
		if ``cov'`cascat'_p'>= 1.645 local `cov'`cascat'_st = "*"
		if ``cov'`cascat'_p'>= 1.960 local `cov'`cascat'_st = "**"
		if ``cov'`cascat'_p'>= 2.576 local `cov'`cascat'_st = "***"
		
loc dec 2
if inlist("`cov'","init_afqsc","init_nrdep") loc dec 3 
local `cov'_`cascat'_se: display %5.`dec'f ``cov'_`cascat'_se'
if ``cov'_`cascat'_se' < 10 & `dec' == 2 local `cov'_`cascat'_se = substr("``cov'_`cascat'_se'",-4,.)

}

	
	cap file close b
	file open b using "${outputfolder}/TableA20.tex", write replace
foreach cov of varlist black hisp other_race married init_nrdep hsgplus init_approx_age init_afqsc { 
if "`cov'" == "black" gl axislab Black
if "`cov'" == "hisp" gl axislab Hispanic
if "`cov'" == "other_race" gl axislab Other Race
if "`cov'" == "married" gl axislab Married
if "`cov'" == "init_nrdep" gl axislab Num. Dependents 
if "`cov'" == "hsgplus" gl axislab HS Graduate +
if "`cov'" == "init_approx_age" gl axislab Age
if "`cov'" == "init_afqsc" gl axislab AFQT
	loc dec2 2
	if inlist("`cov'","init_afqsc","init_nrdep") loc dec2 3

	file write b "${axislab}" _char(38) %5.`dec2'f (``cov'_`INST'_b') ("``cov'`INST'_st'") _char(38) %5.`dec2'f (``cov'_`cascat'_b') ("``cov'`cascat'_st'")  _char(92) _char(92) _n
file write b "" _char(38) _char(40) "``cov'_`INST'_se'" _char(41) _char(38)  _char(40) "``cov'_`cascat'_se'" _char(41) _char(92) _char(92) _n
}
	file write b "\hline" _n
	file write b "Observations & \multicolumn{2}{c}{" %9.`dec'gc (`n') "}" _char(92) _char(92) _n
	file write b "P-value on (Dep Inst) $=$ 0 & \multicolumn{2}{c}{"  %5.`dec'f (`jointF_base') "}" _char(92) _char(92) _n
	file write b "P-value on (Dep Inst x Peer Cas) $=$ 0 & \multicolumn{2}{c}{" %5.`dec'f (`jointF_inter') "}"  _char(92) _char(92) _n
	file write b "P-value on (Dep Inst) $=$ (Dep Inst x Peer Cas) $=$ 0 & \multicolumn{2}{c}{"  %5.`dec'f (`jointF_combined') "}"  _char(92) _char(92) _n
	file close b



}
}



}
}

restore



*************
* Table A21  
*************

	cap drop any_tinitus_in* any_knee_in* any_ptsd_in* any_back_in* any_ankle_in*  any_amputation_in*
	merge 1:1 pid using "./raw_dta/vadc_diag_analysis_samp_ajh" 
	drop _merge 

foreach outcomes in any_tinitus_in any_knee_in any_ptsd_in any_back_in any_ankle_in  any_amputation_in {
	foreach x in 8 16 24 32 { 
	replace `outcomes'_`x' = 0 if `outcomes'_`x' == . & ${rest} >= `x'
	replace `outcomes'_`x' = `outcomes'_`x' * 100
	}
}

foreach x in 8 16 24 32 { 
	summ wia_vser_by_`x'
	if r(max) != 100 {
	replace wia_vser_by_`x' = wia_vser_by_`x'*100
	}
	summ anywl_in_`x'
	if r(max) != 100 {
	replace anywl_in_`x' = anywl_in_`x'*100
	}
	}
	
global RUNLIST any_tinitus_in_ any_knee_in_ any_ptsd_in_ any_back_in_ any_ankle_in_  wia_serplus_by_  wia_vser_by_ any_amputation_in_ anywl_in_ anyssdissi_in_ amtssissdi_in_

local loop = 0 
eststo clear
qui {
foreach dv in $RUNLIST {
	local loop = `loop' + 1
	foreach inst2 in  casnotsd { 

foreach x in 32 {
if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
ivreghdfe `dv'`x' (${NEWENDOG} ${NEWENDOG}_`inst2' = ${NEWINST} ${NEWINST}_`inst2') if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
}
if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
	ivreghdfe `dv' (${NEWENDOG} ${NEWENDOG}_`inst2' = ${NEWINST} ${NEWINST}_`inst2') if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
}


cap drop temp
gen temp = 1 if e(sample)
summ casnotsd if temp == 1 
	local `dv'`x'_b1 = _b[${NEWENDOG}] * 10 
	local `dv'`x'_se1 = _se[${NEWENDOG}] * 10 
	local `dv'`x'_b2 = _b[${NEWENDOG}_`inst2'] * 10 * r(sd)
	local `dv'`x'_se2 = _se[${NEWENDOG}_`inst2'] * 10 * r(sd)

	
if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
reghdfe `dv'`x' ${NEWENDOG} if temp == 1, absorb($FE1) $SEa
}
if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
	reghdfe `dv' ${NEWENDOG} if temp == 1, absorb($FE1) $SEa
}
	local `dv'`x'_b3 = _b[${NEWENDOG}] * 10
	local `dv'`x'_se3 = _se[${NEWENDOG}] * 10


	
		if substr("`dv'",-3,3) == "by_" | substr("`dv'",-3,3) == "in_" reg `dv'`x' if e(sample)
		if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" reg `dv' if e(sample)
		local `dv'_m = _b[_cons]
		
if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) `inst2' if ${rest} >= `x' & temp, absorb($FE1) robust $ivregsettings
}
if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
	ivreghdfe `dv' (${NEWENDOG} = ${NEWINST}) `inst2' if ${rest} >= `x' & temp, absorb($FE1) robust $ivregsettings
}

summ casnotsd if e(sample) == 1 
	local `dv'`x'_b4 = _b[${NEWENDOG}] * 10
	local `dv'`x'_se4 = _se[${NEWENDOG}] * 10
	local `dv'`x'_b5 = _b[`inst2'] * r(sd)
	local `dv'`x'_se5 = _se[`inst2'] * r(sd)

	local dec 2
	
	forvalues i = 1/5 {
		
		local `dv'`x'_p`i' = abs(``dv'`x'_b`i''/``dv'`x'_se`i'')
		if ``dv'`x'_p`i'' < 1.645 local `dv'`x'_st`i' = ""
		if ``dv'`x'_p`i''>= 1.645 local `dv'`x'_st`i' = "*"
		if ``dv'`x'_p`i''>= 1.96 local `dv'`x'_st`i' = "**"
		if ``dv'`x'_p`i''>= 2.576 local `dv'`x'_st`i' = "***"
		
		local `dv'`x'_se`i': display %5.`dec'f ``dv'`x'_se`i''
		if ``dv'`x'_se`i'' < 10 & `dec' == 2 local `dv'`x'_se`i' = substr("``dv'`x'_se`i''",-4,.)
		}

	if "`dv'" == "any_tinitus_in_" global axislab "Tinnitus"
	if "`dv'" == "any_knee_in_" global axislab "Limitation of flexion, knee"
	if "`dv'" == "any_ptsd_in_" global axislab "PTSD"
	if "`dv'" == "any_back_in_" global axislab "Lumbosacral or cervical strain"
	if "`dv'" == "any_ankle_in_" global axislab "Limitation of motion of the ankle"
	if "`dv'" == "wia_serplus_by_" global axislab "Serious or Very Serious Combat Injury"
	if "`dv'" == "wia_vser_by_" global axislab "Very Serious Combat Injury"
	if "`dv'" == "any_amputation_in_" global axislab "Any VADC w/ Amputation"
	if "`dv'" == "anywl_in_" global axislab "Any VADC IU or CDR 100"
	if "`dv'" == "anyssdissi_in_" global axislab "Any SSI or SSDI"
	if "`dv'" == "amtssissdi_in_" global axislab "Amount SSI or SSDI"

	file open b using "${outputfolder}/TableA21_row`loop'.tex", write replace
	
	file write b "${axislab}" _char(38) %6.`dec'f (``dv'`x'_b1') ("``dv'`x'_st1'") _char(38) %6.`dec'f (``dv'`x'_b2') ("``dv'`x'_st2'") _char(38) %6.`dec'f (``dv'_m') _char(92) _char(92) _n
file write b "" _char(38) _char(40) "``dv'`x'_se1'" _char(41) _char(38) _char(40) "``dv'`x'_se2'" _char(41) _char(38) _char(92) _char(92) _n

	file close b
}

}
}
}


*************
* Table A22
*************
	global RUNLIST combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_ noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ bar_or_misconduct_by_ inc_combined_by_ vantage_v4_score731  nsc_enr_postarr_by_ nsc_asscplus_postarr_by_ 

	gen endog_combat = ${NEWENDOG} * combat_occ
	gen inst_combat = ${NEWINST} * combat_occ
	
eststo clear
local loop = 0 
 {
foreach dv in $RUNLIST { 
local loop = `loop' + 1 

foreach x in 32 { 
foreach grps in combat noncombat  { 
if "`grps'" == "combat" local grp = 1
if "`grps'" == "noncombat" local grp = 2
if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST} ) if ${rest} >= `x' & `grps'_occ == 1 , absorb($FE1) robust $ivregsettings
}
if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
	ivreghdfe `dv' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' & `grps'_occ == 1 , absorb($FE1) robust $ivregsettings

}



cap drop temp
gen temp = 1 if e(sample)
	local `dv'`x'_`grp'b = _b[${NEWENDOG}] * 10
	local `dv'`x'_`grp'se = _se[${NEWENDOG}] * 10
	
		if substr("`dv'",-3,3) == "by_" | substr("`dv'",-3,3) == "in_" reg `dv'`x' if temp == 1
		if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" reg `dv' if temp == 1
		local `dv'_`grp'm = _b[_cons]
		
		local dec 2
		local `dv'`x'_`grp'se: display %5.`dec'f ``dv'`x'_`grp'se'
		if ``dv'`x'_`grp'se' < 10 & `dec' == 2 local `dv'`x'_`grp'se = substr("``dv'`x'_`grp'se'",-4,.)
		
		local `dv'`x'_`grp'p = abs(``dv'`x'_`grp'b'/``dv'`x'_`grp'se')
		if ``dv'`x'_`grp'p' < 1.645 local `dv'`x'_`grp'st = ""
		if ``dv'`x'_`grp'p'>= 1.645 local `dv'`x'_`grp'st = "*"
		if ``dv'`x'_`grp'p'>= 1.960 local `dv'`x'_`grp'st = "**"
		if ``dv'`x'_`grp'p'>= 2.576 local `dv'`x'_`grp'st = "***"
	
}

if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
ivreghdfe `dv'`x' (${NEWENDOG} endog_combat  = ${NEWINST} inst_combat ) combat_occ if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
local `dv'_p = (2*ttail(e(df_r), abs(_b[endog_combat]/_se[endog_combat])))
}

if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
ivreghdfe `dv' (${NEWENDOG} endog_combat  = ${NEWINST} inst_combat ) combat_occ if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
local `dv'_p = (2*ttail(e(df_r), abs(_b[endog_combat]/_se[endog_combat])))

}


if "`dv'" == "combat_death_by_" global axislab "Combat Death"
if "`dv'" == "wia_by_" global axislab "Ever Combat Injury"
if "`dv'" == "any_sigprofile_by_" global axislab "Significant Army Profile"
if "`dv'" == "amtvadc_in_" global axislab "Annual Amt VADC"
if "`dv'" == "anyvadc_in_" global axislab "Any VADC Receipt"
if "`dv'" == "noncom_death_by_" global axislab "Noncombat Death"
if "`dv'" == "ndi_despair2_by_" global axislab "Death of Despair"
if "`dv'" == "ndi_suicide_plus2_by_" global axislab "Suicide"
if "`dv'" == "ndi_drug2_by_" global axislab "Drug- or Alcohol-Rel. Death"
if "`dv'" == "ndi_mv_acc2_by_" global axislab "Motor Vehicle Death"
if "`dv'" == "bar_or_misconduct_by_" global axislab "Separated for Misconduct/Barred"
if "`dv'" == "inc_combined_by_" global axislab "Ever Incarcerated"
if "`dv'" == "vantage_v4_score731" global axislab "Credit Score in 2020 (Vantage)"
if "`dv'" == "nsc_enr_postarr_by_" global axislab "Enroll Post-arrival"
if "`dv'" == "nsc_asscplus_postarr_by_" global axislab "Assc Deg+ Post-arrival"
	
	cap file close b
	file open b using "${outputfolder}/TableA22_row`loop'.tex", write replace
	
	file write b "${axislab}" _char(38) %5.`dec'f (``dv'_1m') _char(38) %5.`dec'f (``dv'`x'_1b') ("``dv'`x'_1st'") _char(38) %5.`dec'f (``dv'_2m') _char(38) %5.`dec'f (``dv'`x'_2b') ("``dv'`x'_2st'")  _char(38) %5.4f (``dv'_p') _char(92) _char(92) _n
file write b "" _char(38) _char(38) _char(40) "``dv'`x'_1se'" _char(41) _char(38) _char(38) _char(40) "``dv'`x'_2se'" _char(41) _char(38) _char(92) _char(92) _n


	file close b

}
}
}





*************
* Table A23  
*************


use "created_dta/${analysissamp}", clear
g mths_dep_32 = mths_dep_win_3yr
set seed 1987
global runlist mths_dep_ anyvadc_in_ amtvadc_in_ noncom_death_by_   
global asof 32 
keep if ${rest} >= 32

egen bctyr = group(dmsl_tc_first qtr)
summ bctyr
global bctcnt 810 

preserve
bys bctyr: egen cnt = total(1)
keep bctyr year_arrival qtr cnt 
duplicates drop
isid bctyr
tempfile binfo 
save `binfo', replace
restore

levelsof bctyr, local(a)
foreach bctyr in `a' {
g bctyr_`bctyr' = (bctyr==`bctyr')
g mdXbctyr_`bctyr' = mths_dep_win_3yr * (bctyr==`bctyr')
}

preserve
collapse mths_dep_win_3yr, by(bctyr)
summ mths_dep_win_3yr, det 
restore

preserve
clear 
set obs $bctcnt
g bctyr = _n 
foreach outcome in $runlist {
g b`outcome' = . 
g se`outcome' = . 
}
merge 1:1 bctyr using `binfo', keep(1 3) nogen
save "created_dta/coefs_rf_qtr", replace
restore

foreach outcome in $runlist {
	
cap drop bctyr615 //  Largest, 3rd Cavalry Regiment Fort Hood in 2006Q3.
cap drop mdXbctyr615

local end = $bctcnt - 1

set seed 1987
reghdfe `outcome'${asof}  bctyr_* if ${rest} >= ${asof}, absorb($FE1) vce(robust) 
	preserve
	use "created_dta/coefs_rf_qtr", clear
	forvalues bct = 1(1)`end' {
	replace b`outcome' = _b[bctyr_`bct'] if bctyr == `bct'
	replace se`outcome' = _se[bctyr_`bct'] if bctyr == `bct'
	}
	save "created_dta/coefs_rf_qtr", replace 
	restore
}

use "created_dta/coefs_rf_qtr", clear  
foreach var of varlist * {
	replace `var' = . if `var' == 0 
}

cap log close
log using "${outputfolder}/TableA23_log", text replace

foreach outcome in mths_dep_ anyvadc_in_ amtvadc_in_ noncom_death_by_ {
	* Compute variance / SD and save to log
	capture drop tmp_*
	gen tmp_s2 = se`outcome'^2
	qui su b`outcome', d
	local varest = r(Var)
	qui su tmp_s2
	local var_bct_fx = `varest' - r(mean)
	local var_bct_fx: display %12.2f `var_bct_fx'
	local sd_bct_fx = (`var_bct_fx')^0.5
	local sd_bct_fx: display %12.2f `sd_bct_fx'
	
	* Standard error for variance
	gen tmp_s4 = se`outcome'^4
	egen tmp_mean = mean(b`outcome')
	gen tmp_se = 2*tmp_s4 + 4*((b`outcome' - tmp_mean)^2 - tmp_s2)*tmp_s2
	qui su tmp_se
	local var_se = (r(mean)/r(N))^0.5
	local var_se: display %12.2f `var_se'
	local sd_se = 1/2*`var_se'/`sd_bct_fx'
	local sd_se: display %12.2f `sd_se'

	* Display results
	di "Estimated Var of effects for `outcome'"
	di "Variance: `var_bct_fx' (`var_se')"
	di "SD: `sd_bct_fx' (`sd_se')"

	
	* Compute variance / SD and save to log if n >= 100
	capture drop tmp_*
	gen tmp_s2 = se`outcome'^2  if cnt >= 100
	qui su b`outcome' if cnt >= 100, d
	local varest = r(Var)
	qui su tmp_s2  if cnt >= 100
	local var_bct_fx = `varest' - r(mean)
	local var_bct_fx: display %12.2f `var_bct_fx'
	local sd_bct_fx = (`var_bct_fx')^0.5
	local sd_bct_fx: display %12.2f `sd_bct_fx'
	
	* Standard error for variance
	gen tmp_s4 = se`outcome'^4  if cnt >= 100
	egen tmp_mean = mean(b`outcome') if cnt >= 100
	gen tmp_se = 2*tmp_s4 + 4*((b`outcome' - tmp_mean)^2 - tmp_s2)*tmp_s2  if cnt >= 100
	qui su tmp_se  if cnt >= 100 
	local var_se = (r(mean)/r(N))^0.5
	local var_se: display %12.2f `var_se'
	local sd_se = 1/2*`var_se'/`sd_bct_fx'
	local sd_se: display %12.2f `sd_se'

	* Display results
	di "Estimated Var of effects for `outcome' for n >= 100"
	di "Variance: `var_bct_fx' (`var_se')"
	di "SD: `sd_bct_fx' (`sd_se')"
}

cap log close
	



*************
* Table A24  
*************
		
use "created_dta/${analysissamp}", clear

forv i = 1/36 {
	gen amtssissdi_in_`i' = amtdisability_in_`i' - amtvadc_in_`i'
}	

global RUNLIST  combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_ noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ bar_or_misconduct_by_  inc_combined_by_  nsc_enr_postarr_by_ nsc_asscplus_postarr_by_ vantage_v4_score731


eststo clear
	local loop = 0 
foreach dv in $RUNLIST {
	local loop = `loop'+ 1
	foreach inst2 in casnotsd  {
		if "`inst2'" == "wiarate" local inst2lab = "Peer WIA"
		if "`inst2'" == "casrate" local inst2lab = "Peer Casualties"


foreach x in 32 { 
	
	
if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
	ivreghdfe `dv'`x' (${NEWENDOG} ${NEWENDOG}_`inst2' = ${NEWINST} ${NEWINST}_`inst2') if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
}
if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
	ivreghdfe `dv' (${NEWENDOG} ${NEWENDOG}_`inst2' = ${NEWINST} ${NEWINST}_`inst2') if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
}

cap drop temp
gen temp = 1 if e(sample)
summ casnotsd if temp == 1 
	local `dv'`x'_b1 = _b[${NEWENDOG}] * 10 
	local `dv'`x'_se1 = _se[${NEWENDOG}] * 10 
	local `dv'`x'_b2 = _b[${NEWENDOG}_`inst2'] * 10 * r(sd)
	local `dv'`x'_se2 = _se[${NEWENDOG}_`inst2'] * 10 * r(sd)
	local `dv'`x'_n1 = `r(N)'

if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
	reghdfe `dv'`x' ${NEWENDOG} if temp == 1, absorb($FE1) $SEa
}
if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
	reghdfe `dv' ${NEWENDOG} if temp == 1, absorb($FE1) $SEa
}
	local `dv'`x'_b3 = _b[${NEWENDOG}] * 10
	local `dv'`x'_se3 = _se[${NEWENDOG}] * 10
	
		if substr("`dv'",-3,3) == "by_" | substr("`dv'",-3,3) == "in_" reg `dv'`x' if e(sample)
		if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" reg `dv' if e(sample)
		local `dv'_m = _b[_cons]
	
if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
	ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) `inst2' if ${rest} >= `x' & temp, absorb($FE1) robust $ivregsettings
}
if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
	ivreghdfe `dv' (${NEWENDOG} = ${NEWINST}) `inst2' if ${rest} >= `x' & temp, absorb($FE1) robust $ivregsettings
}

summ casnotsd if e(sample) == 1 
	local `dv'`x'_b4 = _b[${NEWENDOG}] * 10
	local `dv'`x'_se4 = _se[${NEWENDOG}] * 10
	local `dv'`x'_b5 = _b[`inst2'] * r(sd)
	local `dv'`x'_se5 = _se[`inst2'] * r(sd)
	local `dv'`x'_n2 = `r(N)'

	
if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
	ivreghdfe `dv'`x' (${NEWENDOG} ${NEWENDOG}_`inst2' = ${NEWINST} ${NEWINST}_`inst2') `inst2' if ${rest} >= `x' & temp, absorb($FE1) robust $ivregsettings
}
if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
	ivreghdfe `dv' (${NEWENDOG} ${NEWENDOG}_`inst2' = ${NEWINST} ${NEWINST}_`inst2') `inst2' if ${rest} >= `x' & temp, absorb($FE1) robust $ivregsettings
}
summ casnotsd if e(sample) == 1 
	local `dv'`x'_b6 = _b[${NEWENDOG}] * 10 //just dep
	local `dv'`x'_se6 = _se[${NEWENDOG}] * 10
	local `dv'`x'_b7 = _b[`inst2'] * r(sd) // just PC
	local `dv'`x'_se7 = _se[`inst2'] * r(sd)
	local `dv'`x'_b8 = _b[${NEWENDOG}_`inst2'] * 10 * r(sd) // interaction
	local `dv'`x'_se8 = _se[${NEWENDOG}_`inst2'] * 10 * r(sd)
	local `dv'`x'_n3 = `r(N)'

if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
	ivreghdfe `dv'`x' c.casnotsd (c.${NEWENDOG} c.${NEWENDOG}#c.casnotsd = c.${NEWINST} c.${NEWINST}#c.casnotsd) if ${rest} >= `x', absorb($FE1) robust $ivregsettings
	margins, dydx(casnotsd) at(${NEWENDOG}=10) post force
}

if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_"  {
	ivreghdfe `dv' c.casnotsd (c.${NEWENDOG} c.${NEWENDOG}#c.casnotsd = c.${NEWINST} c.${NEWINST}#c.casnotsd) if ${rest} >= `x', absorb($FE1) robust $ivregsettings
	margins, dydx(casnotsd) at(${NEWENDOG}=10) post force
}

summ casnotsd if e(sample) == 1 
	local `dv'`x'_b9 = _b[`inst2'] * r(sd)
	local `dv'`x'_se9 = _se[`inst2'] * r(sd)
	
	local dec 2
	if inlist("`dv'", "amtvadc_in_") local dec 0 
	
	
	forvalues i = 1/9 {
		local `dv'`x'_p`i' = abs(``dv'`x'_b`i''/``dv'`x'_se`i'')
		if ``dv'`x'_p`i'' < 1.645 local `dv'`x'_st`i' = ""
		if ``dv'`x'_p`i''>= 1.645 local `dv'`x'_st`i' = "*"
		if ``dv'`x'_p`i''>= 1.96 local `dv'`x'_st`i' = "**"
		if ``dv'`x'_p`i''>= 2.576 local `dv'`x'_st`i' = "***"
		}
		
	forv i = 1/9 {
		local `dv'`x'_se`i': display %5.`dec'f ``dv'`x'_se`i''
		if ``dv'`x'_se`i'' < 10 & `dec' == 2 local `dv'`x'_se`i' = substr("``dv'`x'_se`i''",-4,.)
		if `dec' == 0 local `dv'`x'_se`i' = substr("``dv'`x'_se`i''",strrpos("``dv'`x'_se`i''"," ")+1,.)
	}
if "`dv'" == "combat_death_by_" global axislab "Combat Death"
if "`dv'" == "wia_by_" global axislab "Ever Combat Injury"
if "`dv'" == "any_sigprofile_by_" global axislab "Significant Army Profile"
if "`dv'" == "amtvadc_in_" global axislab "Annual Amt VADC"
if "`dv'" == "anyvadc_in_" global axislab "Any VADC Receipt"
if "`dv'" == "noncom_death_by_" global axislab "Noncombat Death"
if "`dv'" == "ndi_despair2_by_" global axislab "Death of Despair"
if "`dv'" == "ndi_suicide_plus2_by_" global axislab "Suicide"
if "`dv'" == "ndi_drug2_by_" global axislab "Drug- or Alcohol-Rel. Death"
if "`dv'" == "ndi_mv_acc2_by_" global axislab "Motor Vehicle Death"
if "`dv'" == "bar_or_misconduct_by_" global axislab "Separated for Misconduct/Barred"
if "`dv'" == "inc_combined_by_" global axislab "Ever Incarcerated"
if "`dv'" == "vantage_v4_score731" global axislab "Credit Score in 2020 (Vantage)"
if "`dv'" == "nsc_enr_postarr_by_" global axislab "Enroll Post-arrival"
if "`dv'" == "nsc_asscplus_postarr_by_" global axislab "Assc Deg+ Post-arrival"

		file open b using "${outputfolder}/TableA24_row`loop'.tex", write replace
	
		file write b "${axislab}" _char(38) %6.`dec'f (``dv'_m') _char(38) %6.`dec'f (``dv'`x'_b1') ("``dv'`x'_st1'") _char(38) %6.`dec'f (``dv'`x'_b2') ("``dv'`x'_st2'") _char(38) _char(38) %6.`dec'f (``dv'`x'_b4') ("``dv'`x'_st4'") _char(38) %6.`dec'f (``dv'`x'_b5') ("``dv'`x'_st5'") _char(38) _char(38) %6.`dec'f (``dv'`x'_b6') ("``dv'`x'_st6'") _char(38) %6.`dec'f (``dv'`x'_b7') ("``dv'`x'_st7'") _char(38) %6.`dec'f (``dv'`x'_b8') ("``dv'`x'_st8'") _char(38) %6.`dec'f (``dv'`x'_b9') ("``dv'`x'_st9'") _char(92) _char(92) _n
		file write b "" _char(38) _char(38) _char(40) "``dv'`x'_se1'" _char(41) _char(38) _char(40) "``dv'`x'_se2'" _char(41) _char(38) _char(38) _char(40) "``dv'`x'_se4'" _char(41) _char(38) _char(40) "``dv'`x'_se5'" _char(41) _char(38) _char(38) _char(40) "``dv'`x'_se6'" _char(41) _char(38) _char(40) "``dv'`x'_se7'" _char(41) _char(38) _char(40) "``dv'`x'_se8'" _char(41)  _char(38) _char(40) "``dv'`x'_se9'" _char(41) _char(92) _char(92) _n
		file write b ("\hline") _n 
		file write b "Obs." _char(38) _char(38) _char(38) %6.0fc (``dv'`x'_n1') _char(38) _char(38) _char(38) %6.0fc (``dv'`x'_n2') _char(38) _char(38) _char(38) %6.0fc (``dv'`x'_n3') _char(92) _char(92) _n

		file close b
	

}
}
}



*************
* Table A25
*************

cap program drop getstars
program define getstars, rclass
	syntax, invar(string)
			
	local st ""
	if abs(_b[`invar']/_se[`invar']) >= 1.645 local st "*"
	if abs(_b[`invar']/_se[`invar']) >= 1.96 local st "**"
	if abs(_b[`invar']/_se[`invar']) >= 2.576 local st "***"
			
return local starrer = "`st'"	
end 

preserve 

xtile instgrp = ${NEWINST} if num_qtr_allowed>=32, nq(4)
xtile inst4tile = ${NEWINST} if num_qtr_allowed>=32, nq(4)
xtile inst5tile = ${NEWINST} if num_qtr_allowed>=32, nq(5)
xtile cas4tile = casnotsd if num_qtr_allowed>=32, nq(4)
xtile cas5tile = casnotsd if num_qtr_allowed>=32, nq(5)

gen casgrp = 1 if casnotsd <=0
replace casgrp = 2 if casnotsd>0 & casnotsd<=4
replace casgrp = 3 if casnotsd>4 & casnotsd<=8
replace casgrp = 4 if casnotsd>8 & casnotsd<.

forvalues k=1(1)4 {
	gen instgrp`k' = instgrp == `k'
	gen casgrp`k' = casgrp == `k'
	gen instcasgrp`k' = ${NEWINST}*casgrp`k'
	gen depcasgrp`k' = ${NEWENDOG}*casgrp`k'
}

forvalues k=1(1)4 {
	forvalues j=1(1)4{
		gen inst`k'_cas`j' = (instgrp`k'==1 & casgrp`j'==1)
		gen inst`k'_cas`j'_4tile = (inst4tile==`k' & cas4tile==`j')
	}
}

forvalues k=1(1)5 {
	forvalues j=1(1)5{
		gen inst`k'_cas`j'_5tile = (inst5tile==`k' & cas5tile==`j')
	}
}


local ct = 1 

foreach dv in combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_  noncom_death_by_  ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ bar_or_misconduct_by_  nsc_enr_postarr_by_ nsc_asscplus_postarr_by_ inc_combined_by_ vantage_v4_score731 {

if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
	eststo `dv'32 : ivreghdfe `dv'32 (depcasgrp1 depcasgrp2 depcasgrp3 depcasgrp4 = instcasgrp1 instcasgrp2 instcasgrp3 instcasgrp4) if ${rest} >= 32 , absorb($FE1) robust $ivregsettings
	qui sum `dv'32 if e(sample)
	estadd scalar depmean = r(mean)
}
if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
	eststo `dv' : ivreghdfe `dv' (depcasgrp1 depcasgrp2 depcasgrp3 depcasgrp4 = instcasgrp1 instcasgrp2 instcasgrp3 instcasgrp4) if ${rest} >= 32 , absorb($FE1) robust $ivregsettings
	qui sum `dv' if e(sample)
	estadd scalar depmean = r(mean)
}

if "`dv'" == "combat_death_by_" global axislab "Combat Death"
if "`dv'" == "wia_by_" global axislab "Ever Combat Injury"
if "`dv'" == "any_sigprofile_by_" global axislab "Significant Army Profile"
if "`dv'" == "amtvadc_in_" global axislab "Annual Amt VADC"
if "`dv'" == "anyvadc_in_" global axislab "Any VADC Receipt"
if "`dv'" == "noncom_death_by_" global axislab "Noncombat Death"
if "`dv'" == "ndi_despair2_by_" global axislab "Death of Despair"
if "`dv'" == "ndi_suicide_plus2_by_" global axislab "Suicide"
if "`dv'" == "ndi_drug2_by_" global axislab "Drug- or Alcohol-Rel. Death"
if "`dv'" == "ndi_mv_acc2_by_" global axislab "Motor Vehicle Death"
if "`dv'" == "bar_or_misconduct_by_" global axislab "Separated for Misconduct/Barred"
if "`dv'" == "inc_combined_by_" global axislab "Ever Incarcerated"
if "`dv'" == "vantage_v4_score731" global axislab "Credit Score in 2020 (Vantage)"
if "`dv'" == "nsc_enr_postarr_by_" global axislab "Enroll Post-arrival"
if "`dv'" == "nsc_asscplus_postarr_by_" global axislab "Assc Deg+ Post-arrival"

	
mata: A`ct' = J(2,6,"")
local i = 2
foreach spec in depcasgrp1 depcasgrp2 depcasgrp3 depcasgrp4 {
	getstars, invar(`spec')
	if "`dv'" == "inc_combined_by_" & "`spec'" == "depcasgrp2" {
		local b = strofreal(round(_b[`spec']* ${SCALE}, 0.01), "%9.2f")   + "`r(starrer)'"
	}
	else {
		local b = strofreal(round(_b[`spec']* ${SCALE}, 0.01), "%9.2f")  + "`r(starrer)'"
	}
	local b = strofreal(round(_b[`spec']*${SCALE}, 0.01), "%9.2f")  + "`r(starrer)'"
	local se = "(" + strofreal(round(_se[`spec'] * ${SCALE}, 0.01)  , "%9.2f") + ")"
	mata: A`ct'[.,`i'] = (st_local("b") \ st_local("se"))
	local i = `i' + 1
}
mata: A`ct'[1,1] = st_global("axislab")
local depmean = strofreal(round(`e(depmean)', 0.01), "%9.2f")
mata: A`ct'[1,6] = st_local("depmean")
local ct = `ct' + 1 
}

mata: C = J(1,6,"")
mata: C[1,2] = (" \$D_i $\times$ 1\{W_i = 0\}$")
mata: C[1,3] = (" \$D_i $\times$ 1\{0 < W_i \leq 4\}$")
mata: C[1,4] = (" \$D_i $\times$ 1\{4 < W_i \leq 8\}$")
mata: C[1,5] = (" \$D_i $\times$ 1\{W_i > 8\}$")
mata: C[1,6] = "Dep. Var Mean"

mata: X = A1\ A2\ A3\ A4\ A5
mata: Y = A6\ A7\ A8\ A9\ A10
mata: Z = A11\ A12\ A13\ A14\ A15

cap file close b 
foreach vers in a b c {
	if "`vers'" == "a" local matrix X
	if "`vers'" == "b" local matrix Y
	if "`vers'" == "c" local matrix Z
file open b using "${outputfolder}/TableA25_panel`vers'.tex", write replace 
forv i = 1/10{
	forv j = 1/6{
		mata: st_local("mover", `matrix'[`i', `j'])
		if `j' == 6 {
			file write b ("`mover'") ("\\") _n	
		}
		else {
			file write b ("`mover'") _char(38)
		}
	}
	}
file close b 

}
restore


*************
* Table A26
*************

preserve
	keep if num_qtr_allowed>=32
keep if casnotsd<=0
gen peercas0=1
tempfile peercas0
save `peercas0'
restore

preserve 
keep if num_qtr_allowed>=32
gen peercas0=0
append using `peercas0'

egen fe1_peercas0=group($FE1 peercas0)
gen ${NEWINST}_peercas0 = ${NEWINST}*peercas0
gen ${NEWENDOG}_peercas0 = ${NEWENDOG}*peercas0

reghdfe combat_death_by_32 ${NEWINST} ${NEWINST}_peercas0  ${NEWINST}_casnotsd if ${rest} >= 32, absorb(fe1_peercas0)
keep if e(sample)==1


gen miss_incar=(inc_combined_by_32==.)
replace inc_combined_by_32 = 0 if inc_combined_by_32==.

gen miss_credit=(vantage_v4_score731==.)
sum vantage_v4_score731
replace vantage_v4_score731 = `r(mean)' if vantage_v4_score731==.

gen amtssdissi_in_32 = amtdisability_in_32 - amtvadc_in_32

local loop = 0 
foreach cov of varlist combat_death_by_32 wia_by_32 any_sigprofile_by_32 amtvadc_in_32 anyvadc_in_32  noncom_death_by_32  ndi_despair2_by_32 ndi_suicide_plus2_by_32 ndi_drug2_by_32 ndi_mv_acc2_by_32 bar_or_misconduct_by_32 inc_combined_by_32 vantage_v4_score731 nsc_enr_postarr_by_32 nsc_asscplus_postarr_by_32   { 
	local loop = `loop' + 1 
	
	reghdfe ${NEWENDOG} ${NEWINST} ${NEWINST}_peercas0  ${NEWINST}_casnotsd if ${rest} >= 32, absorb(fe1_peercas0) vce(robust)
	cap drop first_fit_endog
	predict first_fit_endog, xb
	
	reghdfe ${NEWENDOG}_casnotsd ${NEWINST} ${NEWINST}_peercas0  ${NEWINST}_casnotsd if ${rest} >= 32, absorb(fe1_peercas0) vce(robust)
	cap drop first_fit_endog_casnotsd
	predict first_fit_endog_casnotsd, xb
	
	reghdfe ${NEWENDOG}_peercas0 ${NEWINST} ${NEWINST}_peercas0  ${NEWINST}_casnotsd if ${rest} >= 32, absorb(fe1_peercas0) vce(robust)
	cap drop first_fit_endog_peercas0
	predict first_fit_endog_peercas0, xb
	
	areg `cov' , a(fe1_peercas0)
	predict r`cov', resid
	local dof = e(N) - e(k_absorb) - 1
	
	foreach n in endog endog_casnotsd endog_peercas0 {
		areg first_fit_`n', a(fe1_peercas0)
		cap drop rfirst_fit_`n'
		predict rfirst_fit_`n', resid
		
	}
	
	if "`cov'"!= "inc_combined_by_32" & "`cov'"!= "vantage_v4_score731" reg r`cov' rfirst_fit_endog_peercas0 rfirst_fit_endog rfirst_fit_endog_casnotsd, dof(`dof')
	estimates store n`cov'
	if "`cov'"== "inc_combined_by_32"  reg r`cov' rfirst_fit_endog_peercas0 rfirst_fit_endog rfirst_fit_endog_casnotsd miss_incar, dof(`dof')
	estimates store n`cov'
	if "`cov'"== "vantage_v4_score731"  reg r`cov' rfirst_fit_endog_peercas0 rfirst_fit_endog rfirst_fit_endog_casnotsd miss_credit, dof(`dof')
	estimates store n`cov'
	
	ivreghdfe `cov'  (${NEWENDOG} = ${NEWINST} )  if ${rest} >= 32 & peercas0==1, absorb($FE1) robust $ivregsettings
	
	local `cov'_b3 = _b[${NEWENDOG}] * 10 
	local `cov'_se3 = _se[${NEWENDOG}] * 10 
	ivreghdfe `cov' (${NEWENDOG}_peercas0  ${NEWENDOG}  ${NEWENDOG}_casnotsd = ${NEWINST}_peercas0 ${NEWINST} ${NEWINST}_casnotsd)  if ${rest} >= 32, absorb(fe1_peercas0) robust $ivregsettings
	
	local p_5 = 2*ttail(e(df_r),abs(_b[${NEWENDOG}_peercas0]/_se[${NEWENDOG}_peercas0]))
	
	ivreghdfe `cov' (${NEWENDOG}  ${NEWENDOG}_casnotsd = ${NEWINST}  ${NEWINST}_casnotsd)  if ${rest} >= 32 & peercas0==0, absorb(fe1_peercas0) robust first $ivregsettings
	
	cap drop temp
	gen temp = 1 if e(sample)
	summ casnotsd if temp == 1 
		local `cov'_b1 = _b[${NEWENDOG}] * 10 
		local `cov'_se1 = _se[${NEWENDOG}] * 10 
		local `cov'_b2 = _b[${NEWENDOG}_casnotsd] * 10 * r(sd)
		local `cov'_se2 = _se[${NEWENDOG}_casnotsd] * 10 * r(sd)
	
	if substr("`cov'",-3,3) == "by_" |  substr("`cov'",-3,3) == "in_"  {
	reghdfe `cov' ${NEWENDOG} if temp == 1, absorb($FE1) $SEa
	}
	if substr("`cov'",-3,3) != "by_" &  substr("`cov'",-3,3) != "in_" {
		reghdfe `cov' ${NEWENDOG} if temp == 1, absorb($FE1) $SEa
	}
		if substr("`cov'",-3,3) == "by_" | substr("`cov'",-3,3) == "in_" reg `cov' if e(sample)
		if substr("`cov'",-3,3) != "by_" &  substr("`cov'",-3,3) != "in_" reg `cov' if e(sample)
		local `cov'_m = _b[_cons]
		
	local dec 2
	local decpval 3
	if inlist("`cov'","ndi_mv_acc2_by_") local dec 3
	if inlist("`cov'", "amtvadc_in_") local dec 0 

	forvalues i = 1/3 {
		local `cov'_p`i' = abs(``cov'_b`i''/``cov'_se`i'')
		if ``cov'_p`i'' < 1.645 local `cov'_st`i' = ""
		if ``cov'_p`i''>= 1.645 local `cov'_st`i' = "*"
		if ``cov'_p`i''>= 1.96 local `cov'_st`i' = "**"
		if ``cov'_p`i''>= 2.576 local `cov'_st`i' = "***"
		}
			
	forv i = 1/3 {
	local `cov'_se`i': display %5.`dec'f ``cov'_se`i''
	if ``cov'_se`i'' < 10 & `dec' == 2 local `cov'_se`i' = substr("``cov'_se`i''",-4,.)
	if `dec' == 0 local `cov'_se`i' = substr("``cov'_se`i''",strrpos("``cov'_se`i''"," ")+1,.)
	}
	
if "`cov'" == "combat_death_by_32" global axislab "Combat Death"
if "`cov'" == "wia_by_32" global axislab "Ever Combat Injury"
if "`cov'" == "any_sigprofile_by_32" global axislab "Significant Army Profile"
if "`cov'" == "amtvadc_in_32" global axislab "Annual Amt VADC"
if "`cov'" == "anyvadc_in_32" global axislab "Any VADC Receipt"
if "`cov'" == "noncom_death_by_32" global axislab "Noncombat Death"
if "`cov'" == "ndi_despair2_by_32" global axislab "Death of Despair"
if "`cov'" == "ndi_suicide_plus2_by_32" global axislab "Suicide"
if "`cov'" == "ndi_drug2_by_32" global axislab "Drug- or Alcohol-Rel. Death"
if "`cov'" == "ndi_mv_acc2_by_32" global axislab "Motor Vehicle Death"
if "`cov'" == "bar_or_misconduct_by_32" global axislab "Separated for Misconduct/Barred"
if "`cov'" == "inc_combined_by_32" global axislab "Ever Incarcerated"
if "`cov'" == "vantage_v4_score731" global axislab "Credit Score in 2020 (Vantage)"
if "`cov'" == "nsc_enr_postarr_by_32" global axislab "Enroll Post-arrival"
if "`cov'" == "nsc_asscplus_postarr_by_32" global axislab "Assc Deg+ Post-arrival"

	file open b using "${outputfolder}/TableA26_row`loop'.tex", write replace
	
	file write b "${axislab}" _char(38) %6.`dec'f (``cov'_b1') ("``cov'_st1'") _char(38) %6.`dec'f (``cov'_b2') ("``cov'_st2'") _char(38) _char(38) %6.`dec'f (``cov'_b3') ("``cov'_st3'") _char(38) _char(38) %6.`decpval'f (`p_5') _char(92) _char(92) _n
	file write b "" _char(38) _char(40) "``cov'_se1'" _char(41) _char(38) _char(40) "``cov'_se2'" _char(41) _char(38) _char(38) _char(40) "``cov'_se3'" _char(41) _char(38) _char(38)  _char(92) _char(92) _n
	
	file close b
}

restore


use "created_dta/${wholesamp}", clear
forv i = 1/36{
	gen amtssissdi_in_`i' = amtdisability_in_`i' - amtvadc_in_`i'
}


*************
* Table A27 & A28 
*************
	global Xs init_approx_age init_white init_black init_hispanic female afqs_mi  any_moral_waiver init_afqsc init_afqsc_2 init_afqsc_3 married init_nrdep hsd_ged high_school  any_meps_med_fail_cd any_meps_alcohol_fail  any_meps_marijuana_fail any_meps_cocaine_fail any_meps_alcohol_tst_miss any_meps_marijuana_tst_miss any_meps_cocaine_tst_miss ace_first_asvabcl ace_first_asvabco ace_first_asvabel ace_first_asvabfa ace_first_asvabgm ace_first_asvabgt ace_first_asvabmm ace_first_asvabof ace_first_asvabsc cl_mi co_mi el_mi fa_mi gm_mi gt_mi mm_mi of_mi sc_mi
	
foreach wspc in twoinst_cas { 
foreach fe in FE1_as { 
foreach dvx in wia_by_ noncom_death_by_ amtvadc_in_  amtssissdi_in_ combat_death_by_ ndi_suicide_plus2_by_ ndi_despair2_by_ anyptsd_in_ {
	foreach timehorizon in 8 { 
local x = 4*(`timehorizon')

local dv `dvx'

preserve
local yr1 2001
if inlist("`dv'","inc_combined_by_","rec_any_forc_by_") {
	drop if year_arrival == 2001
	local yr1 2002
}

local midyr 2006 

if inlist("`dv'","inc_combined_by_","noncom_death_by_","ndi_despair2_by_","ndi_suicide_plus2_by_") local midyr 2008
if inlist("`dv'","ndi_war_by_","combat_death_by_","wia_by_") local midyr 2005

local endyr = 2019 - `timehorizon'

if "`wspc'" == "twoinst_cas" noi ivreghdfe `dv'`x' $Xs an_samp_dum (${NEWENDOG} ${NEWENDOG}_casrate  = ${NEWINST} ${NEWINST}_casrate ) if ${rest} >= `x' , absorb(FE = ${`fe'}) robust $ivregsettings


keep if e(sample)


predict pred_X_and_dep 
if "`wspc'" == "twoinst_cas" {
g pred_dep = _b[${NEWENDOG}]*${NEWENDOG} + _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate
g pred_X_no_dep = pred_X_and_dep -  _b[${NEWENDOG}]*${NEWENDOG} - _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate
}
g pred_FE = FE

g C = 1
noi summ year_arrival
collapse (mean)   `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep pred_FE  (sum) C, by(year_arrival)

noi tab year_arrival
noi list *

foreach v in `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep pred_FE  {
summ `v' if year_arrival == `yr1' 
replace `v'= `v'-r(mean)
}

noi list * 

	foreach y in `yr1' `midyr' `endyr' { 
	gen Y`y'_ = `dv'`x' if year_arrival == `y'
	egen Y`y' = max(Y`y'_)
	drop Y`y'_ 
	gen pX`y'_ = pred_X_no_dep if year_arrival == `y'
	egen pX`y' = max(pX`y'_)
	drop pX`y'_ 
	gen pdep`y'_ = pred_dep if year_arrival == `y'
	egen pdep`y' = max(pdep`y'_)
	drop pdep`y'_ 
	gen pdepX`y'_ = pred_X_and_dep if year_arrival == `y'
	egen pdepX`y' = max(pdepX`y'_)
	drop pdepX`y'_ 
	}

	foreach oc in Y pX pdep pdepX { 
	g delta`oc'_0113 = `oc'`endyr' - `oc'`yr1'
	g delta`oc'_0107 = `oc'`midyr' - `oc'`yr1'
	g delta`oc'_0713 = `oc'`endyr' - `oc'`midyr'

	}
	foreach oc in pX pdep pdepX { 
	g frac`oc'_0113 = delta`oc'_0113/deltaY_0113 
	g frac`oc'_0107 = delta`oc'_0107/deltaY_0107 
	g frac`oc'_0713 = delta`oc'_0713/deltaY_0713 
	}

	eststo clear 
	foreach oc in Y pX pdep pdepX {  
	eststo m0113`oc': reg delta`oc'_0113
	eststo m0107`oc': reg delta`oc'_0107
	eststo m0713`oc': reg delta`oc'_0713

	}
	foreach oc in pX pdep pdepX {  
	eststo f0113`oc': reg frac`oc'_0113
	eststo f0107`oc': reg frac`oc'_0107
	eststo f0713`oc': reg frac`oc'_0713
	}


egen varY = var(`dv'`x')  if year_arrival != `yr1' 
correlate `dv'`x' pred_X_no_dep, covariance
g covX = r(cov_12) if year_arrival != `yr1' 
g R2X = covX/varY if year_arrival != `yr1' 

correlate `dv'`x' pred_dep if year_arrival != `yr1' , covariance
g covdep = r(cov_12) if year_arrival != `yr1' 
g R2dep = covdep/varY if year_arrival != `yr1' 

correlate `dv'`x' pred_X_and_dep if year_arrival != `yr1' , covariance
g covXdep = r(cov_12) if year_arrival != `yr1' 
g R2Xdep = covXdep/varY if year_arrival != `yr1' 

		eststo R2X: reg R2X if year_arrival != `yr1' 
		eststo R2dep: reg R2dep if year_arrival != `yr1' 
		eststo R2Xdep: reg R2Xdep if year_arrival != `yr1' 

		
if inlist("`wspc'","IV","twoinst_cas") {
	local deplab = "Dep."
	if "`wspc'" == "twoinst_cas" local deplab = "Dep. and Cas."
	local dec 3
	if inlist("`dv'","amtdisability_in_","amtvadc_in_") local dec 3
	
if "`dvx'" == "wia_by_" local outname A27_panela_row
if "`dvx'" == "noncom_death_by_" local outname A27_panelb_row
if "`dvx'" == "amtvadc_in_" local outname A27_panelc_row
if "`dvx'" == "amtssissdi_in_" local outname A27_paneld_row

if "`dvx'" == "combat_death_by_" local outname A28_panela_row
if "`dvx'" == "ndi_despair2_by_" local outname A28_panelb_row
if "`dvx'" == "ndi_suicide_plus2_by_" local outname A28_panelc_row
if "`dvx'" == "anyptsd_in_" local outname A28_paneld_row


noi esttab m0107Y m0713Y using "${outputfolder}\Table`outname'1.tex",  ///
		cells(b(fmt(`dec') star))  ///
									replace noobs nomtitle nodepvar nonumber plain fragment label ///
									substitute(_ \_) style(tex) ///
									mlabels(, none) collabels(, none) ///
									keep(_cons, relax) ///
									varlabels(_cons "Dep. Variable") ///
									starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30) ///
									stats( )
noi esttab m0107pX m0713pX R2X using "${outputfolder}\Table`outname'2.tex",  ///
		cells(b(fmt(`dec') star))  ///
									replace noobs nomtitle nodepvar nonumber plain fragment label ///
									substitute(_ \_) style(tex) ///
									mlabels(, none) collabels(, none) ///
									keep(_cons, relax) ///
									varlabels(_cons "Xs") ///
									starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30) ///
									stats( )
noi esttab f0107pX f0713pX using "${outputfolder}\Table`outname'3.tex",  ///
		cells(b(fmt(`dec') star))  ///
									replace noobs nomtitle nodepvar nonumber plain fragment label ///
									substitute(_ \_) style(tex) ///
									mlabels(, none) collabels(, none) ///
									keep(_cons, relax) ///
									varlabels(_cons "Frac. Explained by Xs") ///
									starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30) ///
									stats( )

									
	noi esttab m0107pdep m0713pdep R2dep using "${outputfolder}\Table`outname'4.tex",  ///
		cells(b(fmt(`dec') star))  ///
									replace noobs nomtitle nodepvar nonumber plain fragment label ///
									substitute(_ \_) style(tex) ///
									mlabels(, none) collabels(, none) ///
									keep(_cons, relax) ///
									varlabels(_cons "`deplab'") ///
									starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30) ///
									stats( )
									

									
	noi esttab f0107pdep f0713pdep  using "${outputfolder}\Table`outname'5.tex",  ///
		cells(b(fmt(`dec') star))  ///
									replace noobs nomtitle nodepvar nonumber plain fragment label ///
									substitute(_ \_) style(tex) ///
									mlabels(, none) collabels(, none) ///
									keep(_cons, relax) ///
									varlabels(_cons "Frac. Explained by `deplab'") ///
									starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30) ///
									stats( )
									
}

restore
}
}
}
}


*-------------------*
*Tables A29 and A30 contain no data 
*-------------------*


*************
* Table B1 
*************

use "created_dta/${wholesamp}", clear
	merge 1:1 pid using created_dta/analysis_samp_whole_samp, gen(merge1) keep(1 3) keepusing(count_enlisted_36 post_qtr other_asgcons child_soldier abn_volunteer)
	merge 1:1 pid using created_dta/analysis_samp, nogen keep(1 3 4 5) update keepusing(count_enlisted_36) 

	cap gen hsg = init_civedcatg == "HSG"
	cap gen smcplus = inlist(init_civedcatg,"ASC","CLG","GRD","SMC")
	
	gen bct_move = 0 
		replace bct_move = 1 if post_orgraa=="Fort Polk" & dmsl_tc_first=="42I_TC" 
		replace bct_move = 1 if post_orgraa=="Fort Hood" & dmsl_tc_first=="24I_TC"
		replace bct_move = 1 if post_orgraa=="Fort Hood" & dmsl_tc_first=="14I_TC"
		replace bct_move = 1 if post_orgraa=="Fort Lewis" & dmsl_tc_first=="81F_TC"
		replace bct_move = 1 if post_orgraa=="Fort Richardson" & dmsl_tc_first=="SB1_TC" 
	
	gen deactivate_36 =  count_enlisted_36==.|count_enlisted_36<2000
	
	gen bad_mos = inlist(init_pmos_cons,"09B","09S_imp","98X_imp","00D_imp","00G_imp") | inlist(init_pmos_cons,"CAC_imp","CFB_imp","XXX_imp","YYO_imp")

		preserve
		keep if (inrange(year_arrival,2005,2015)==1 & bct_type != "" & inrange(real(init_terms),3,6) == 1 & inlist(init_grade,"E01","E02","E03","E04")== 1 & !(init_phyca_limitations==1 | (other_asgcons==1 & init_asgcons != "V5") | child_soldier==1) & !inlist(post_orgraa,"GERMANY","ITALY","KOREA") & bct_move == 0 & deactivate_36 == 0) | an_samp_dum == 1
		keep post_qtr post_orgraa dmsl_tc_first pid
		duplicates drop post_qtr post_orgraa dmsl_tc_first, force
		sort post_qtr post_orgraa dmsl_tc_first
		bysort post_qtr: egen dmsl_ct_inpost=count(dmsl_tc_first)
		tempfile only_bct_post
		save `only_bct_post'

		restore
		merge m:1 post_qtr dmsl_tc_first using `only_bct_post', keep(1 3) keepusing(dmsl_ct_inpost) nogen
		
		drop BCTXarrivyr_ct
		preserve 
		keep if   (inrange(year_arrival,2005,2015)==1 & bct_type != "" & inrange(real(init_terms),3,6) == 1 & inlist(init_grade,"E01","E02","E03","E04")== 1 & !(init_phyca_limitations==1 | (other_asgcons==1 & init_asgcons != "V5") | child_soldier==1) & !inlist(post_orgraa,"GERMANY","ITALY","KOREA") & bct_move == 0 & deactivate_36 == 0 & dmsl_ct_inpost != 1) | an_samp_dum == 1
		egen BCTXarrivyr_ct=count(pid), by(BCTXarrivyr)
		keep BCTXarrivyr_ct BCTXarrivyr
		duplicates drop BCTXarrivyr, force
		tempfile BCTXarrivyr_ct
		save `BCTXarrivyr_ct'
		restore
		merge m:1 BCTXarrivyr using `BCTXarrivyr_ct', keep(1 3) keepusing(BCTXarrivyr_ct) nogen
		
		
		
	qui  ivreghdfe noncom_death_by_8 (${NEWENDOG}_as = ${NEWINST}_as) if ${rest} >= 8 & an_samp_dum == 1 , absorb(mos_tc_post_yr_terms_an_samp) robust
	g temp_as = 1 if e(sample)
	
	foreach var in init_approx_age married black hisp other_race female init_nrdep hsd_ged hsg smcplus init_afqsc combat_occ mths_dep_win_3yr wia_by_12 combat_death_by_12  {
		foreach grp in full a0515 bct  asgcon loc loc2 unit sing small mos men as2 { 
			local cond 
			if "`grp'" == "a0515" local cond `"if inrange(year_arrival,2005,2015)==1 "'

			if "`grp'" == "bct" local cond `"if   (inrange(year_arrival,2005,2015)==1 & bct_type != "") | an_samp_dum == 1 "'
			
			if "`grp'" == "terms3" local cond `"if   (inrange(year_arrival,2005,2015)==1 & bct_type != "" & inrange(real(init_terms),3,6) == 1 & inlist(init_grade,"E01","E02","E03","E04") == 1) | an_samp_dum == 1 "'
		
		if "`grp'" == "asgcon" local cond `"if   (inrange(year_arrival,2005,2015)==1 & bct_type != "" & inrange(real(init_terms),3,6) == 1 & inlist(init_grade,"E01","E02","E03","E04") == 1 & !(init_phyca_limitations==1 | (other_asgcons==1 & init_asgcons != "V5") | child_soldier==1)) | an_samp_dum == 1"'
		
		if "`grp'" == "loc" local cond `"if   (inrange(year_arrival,2005,2015)==1 & bct_type != "" & inrange(real(init_terms),3,6) == 1 & inlist(init_grade,"E01","E02","E03","E04")== 1 & !(init_phyca_limitations==1 | (other_asgcons==1 & init_asgcons != "V5") | child_soldier==1) & !inlist(post_orgraa,"GERMANY","ITALY","KOREA")) | an_samp_dum == 1"'

		if "`grp'" == "loc2" local cond `"if   (inrange(year_arrival,2005,2015)==1 & bct_type != "" & inrange(real(init_terms),3,6) == 1 & inlist(init_grade,"E01","E02","E03","E04")== 1 & !(init_phyca_limitations==1 | (other_asgcons==1 & init_asgcons != "V5") | child_soldier==1) & !inlist(post_orgraa,"GERMANY","ITALY","KOREA") & bct_move == 0) | an_samp_dum == 1"'
		
		if "`grp'" == "unit" local cond `"if   (inrange(year_arrival,2005,2015)==1 & bct_type != "" & inrange(real(init_terms),3,6) == 1 & inlist(init_grade,"E01","E02","E03","E04")== 1 & !(init_phyca_limitations==1 | (other_asgcons==1 & init_asgcons != "V5") | child_soldier==1) & !inlist(post_orgraa,"GERMANY","ITALY","KOREA") & bct_move == 0 & deactivate_36 == 0) | an_samp_dum == 1"'
		
		
		if "`grp'" == "sing" local cond `"if   (inrange(year_arrival,2005,2015)==1 & bct_type != "" & inrange(real(init_terms),3,6) == 1 & inlist(init_grade,"E01","E02","E03","E04")== 1 & !(init_phyca_limitations==1 | (other_asgcons==1 & init_asgcons != "V5") | child_soldier==1) & !inlist(post_orgraa,"GERMANY","ITALY","KOREA") & bct_move == 0 & deactivate_36 == 0 & dmsl_ct_inpost != 1) | an_samp_dum == 1"'
		
		if "`grp'" == "small" local cond `"if  ( inrange(year_arrival,2005,2015)==1 & bct_type != "" & inrange(real(init_terms),3,6) == 1 & inlist(init_grade,"E01","E02","E03","E04")== 1 & !(init_phyca_limitations==1 | (other_asgcons==1 & init_asgcons != "V5") | child_soldier==1) & !inlist(post_orgraa,"GERMANY","ITALY","KOREA") & bct_move == 0 & deactivate_36 == 0 & BCTXarrivyr_ct>=100 & dmsl_ct_inpost != 1) | an_samp_dum == 1"'
		
		if "`grp'" == "mos" local cond `"if   (inrange(year_arrival,2005,2015)==1 & bct_type != "" & inrange(real(init_terms),3,6) == 1 & inlist(init_grade,"E01","E02","E03","E04")== 1 & !(init_phyca_limitations==1 | (other_asgcons==1 & init_asgcons != "V5") | child_soldier==1) & !inlist(post_orgraa,"GERMANY","ITALY","KOREA") & bct_move == 0 & deactivate_36 == 0 & BCTXarrivyr_ct>=100 & dmsl_ct_inpost != 1 & bad_mos == 0) | an_samp_dum == 1"'
		
		if "`grp'" == "men" local cond `"if   (inrange(year_arrival,2005,2015)==1 & bct_type != "" & inrange(real(init_terms),3,6) == 1 & inlist(init_grade,"E01","E02","E03","E04")== 1 & !(init_phyca_limitations==1 | (other_asgcons==1 & init_asgcons != "V5") | child_soldier==1) & init_sex == "M" & !inlist(post_orgraa,"GERMANY","ITALY","KOREA") & bct_move == 0 & deactivate_36 == 0 & BCTXarrivyr_ct>=100 & dmsl_ct_inpost != 1 & bad_mos == 0) | an_samp_dum == 1"'
		
		if "`grp'" == "as2" local cond `"if (temp_as == 1)"'
		
		summ `var' `cond'
			local `grp'_`var' = r(mean)
			if inlist("`var'","married","black","hisp","other_race","female","hsd_ged") local `grp'_`var' = ``grp'_`var'' * 100
			if inlist("`var'","hsg","smcplus","combat_occ" ) local `grp'_`var' = ``grp'_`var'' * 100
		}
	}


		cap file close b
	file open b using "${outputfolder}/TableB1_panela.tex", write replace
	foreach var in init_approx_age married black hisp other_race female init_nrdep hsd_ged hsg smcplus init_afqsc {
		local dec 2
		if inlist("`var'","init_approx_age","init_nrdep","init_afqsc") local dec 2
		if "`var'" == "init_approx_age" local dvlab "Age"
		if "`var'" == "married" local dvlab "Married"
		if "`var'" == "black" local dvlab "Black"
		if "`var'" == "hisp" local dvlab "Hispanic"
		if "`var'" == "other_race" local dvlab "Other Race"
		if "`var'" == "female" local dvlab "Female"
		if "`var'" == "init_nrdep" local dvlab "Number of Dependents"
		if "`var'" == "hsd_ged" local dvlab "HS Dropout or GED"
		if "`var'" == "hsg" local dvlab "HS Graduate"
		if "`var'" == "smcplus" local dvlab "Some College+"
		if "`var'" == "init_afqsc" local dvlab "AFQT Score"
local tab 	
foreach grp in full a0515 bct  asgcon loc loc2 unit sing small mos men as2 { // terms3
	local tab `tab' _char(38) %5.`dec'f (``grp'_`var'')
}
		file write b "`dvlab'" `tab' _char(92) _char(92) _n
	}
	file close b
	
	file open b using "${outputfolder}/TableB1_panelb.tex", write replace
	foreach var in combat_occ mths_dep_win_3yr wia_by_12 combat_death_by_12 {
		local dec 2
		if inlist("`var'","mths_dep_win_3yr") local dec 2
		if "`var'" == "combat_occ" local dvlab "Combat Occupation"
		if "`var'" == "mths_dep_win_3yr" local dvlab "Mths Deployed w/in 3 yrs"
		if "`var'" == "wia_by_12" local dvlab "Combat Injury w/in 3 yrs"
		if "`var'" == "combat_death_by_12" local dvlab "Combat Death w/in 3 yrs"

local tab 	
foreach grp in full a0515 bct  asgcon loc loc2 unit sing small mos men as2 { // terms3
	local tab `tab' _char(38) %5.`dec'f (``grp'_`var'')
}

		file write b "`dvlab'" `tab' _char(92) _char(92) _n
	}
	file close b


